如何在cmake中的源文件之后设置库标志? [英] How to set library flags after source file in cmake?
问题描述
我正在尝试使用带有CMake的hwloc编译项目.但是,链接时会出现大量未定义的参考错误:
I'm trying to compile a project using hwloc with CMake. However, I get a ton of undefined reference errors when linking:
undefined reference to `hwloc_get_type_depth'
undefined reference to `hwloc_bitmap_zero'
[...]
根据此回答一个类似的问题,标志的顺序很重要.
According to this answer to a similar question the order of flags is important.
那么,如何在CMake中生成这样的命令?:
So, how can I generate a command like this in CMake? :
g ++ -Wall -std = c ++ 11 source.cpp
-lhwloc
g++ -Wall -std=c++11 source.cpp
-lhwloc
从我的CMakeLists.txt中摘录:
Excerpt from my CMakeLists.txt:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 -lhwloc")
set(SOURCE_FILES source.cpp)
add_executable(source ${SOURCE_FILES})
任何帮助将不胜感激!
My question was proposed as a possible duplicate of this one, however the flag I wanted to add was to link against a library and not a normal compile flag as seems to be the case in the above mentioned question. @Edgar Rokyan provided the right answer for my problem.
推荐答案
If you need to link against hwloc library you might use target_link_libraries command:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11") # <== remove *-lhwloc*
set(SOURCE_FILES source.cpp)
add_executable(source ${SOURCE_FILES})
target_link_libraries(source hwloc) # <== add this line
这篇关于如何在cmake中的源文件之后设置库标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!