我正在尝试使用Intel C编译器,CMake和std :: threads支持来编译一个简单的测试项目.
如果我做 :
icpc -std=c++11 -lpthread source/main.cpp
然后,构建和程序运行正常.
但是使用CMake我得到一个错误:
CMake Error at CMakeLists.txt:21 (TARGET_LINK_LIBRARIES):
Cannot specify link libraries for target "test" which is not built by
this project.
CMakeLists.txt是:
project(test)
set (CMAKE_CXX_FLAGS "-std=c++11")
set(SOURCE_LIST "source/main.cpp")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} pthread)
add_executable (${PROJECT_NAME} ${SOURCE_LIST})
我正在由icc(compilervars.sh)提供的脚本设置的环境中构建,并且使用-DCMAKE_C_COMPILER = icc -DCMAKE_CXX_COMPILER = icpc选项调用CMake.如果我不使用线程,它可以工作.
我使用CMake有什么问题?
谢谢!
解决方法:
回顾target_link_libraries
的文档,必须在使用目标之前声明该目标:
The named
<target>
must have been created in the current directory by a command such as 07001 or 07002.
BTW如this answer中所述,您不应显式设置CMAKE_CXX_FLAGS,而应该设置CMAKE_CXX_STANDARD和相关变量:
project(test)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(SOURCE_LIST "source/main.cpp")
add_executable(${PROJECT_NAME} ${SOURCE_LIST})
target_link_libraries(${PROJECT_NAME} pthread)