0

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.

hsvar
  • 177
  • 4
  • 14
  • Possible duplicate of [How to add linker or compile flag in cmake file?](https://stackoverflow.com/questions/11783932/how-to-add-linker-or-compile-flag-in-cmake-file) – Alex Huszagh Jul 01 '17 at 21:33
  • Possible duplicate of [How to add "-l" (ell) compiler flag in CMake](https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake) – Tsyvarev Jul 01 '17 at 22:09

1 Answers1

5

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
Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67