hsvar
hsvar

Reputation: 197

How to set library flags after source file in cmake?

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.

Upvotes: 0

Views: 1427

Answers (1)

Edgar Rokjān
Edgar Rokjān

Reputation: 17483

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

Upvotes: 5

Related Questions