CMake将单个标题文件添加到项目

时间:2017-11-25 00:40:11

标签: c++ cmake header header-only

我特别想在我的项目中添加stb_image_write.h,但在使用CMake进行编译时会出现“多重定义”错误。

我将stb_image_write.h包含在一个名为“screen.h”的头文件中,其中包含如下:

#include <SDL2/SDL.h>
#include <vector>
#include "float3.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

然后我将此标题包含在cpp文件中,如下所示:

#include "screen.h"

这是我的CMake:

cmake_minimum_required(VERSION 3.8)
project(YAPT)

find_package(SDL2 REQUIRED)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lSDL")
if(OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
set(CMAKE_CXX_FLAGS "-fopenmp")
include_directories(SYSTEM ${OpenMP_INCLUDE_PATH})
include_directories("${PROJECT_BINARY_DIR}")
link_directories("${PROJECT_BINARY_DIR}")

file(GLOB SOURCE_FILES "src/*.cpp")
file(GLOB HEADER_FILES  "src/*.h")

add_executable(YAPT ${SOURCE_FILES} ${HEADER_FILES})
target_link_libraries(YAPT SDL2main SDL2)

文件位置与CMakeLists.txt位于同一目录中,但我也在/ src文件夹中尝试过,但没有运气。

0 个答案:

没有答案