How to set library flags after source file in cmake?

﹥>﹥吖頭↗ 提交于 2021-02-04 21:52:45

问题


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.

So, how can I generate a command like this in CMake? :

g++ -Wall -std=c++11 source.cpp-lhwloc

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})

Any help is greatly appreciated!

Edit: 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.


回答1:


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


来源:https://stackoverflow.com/questions/44865528/how-to-set-library-flags-after-source-file-in-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!