(Linux) Unable to link archives through cmake

At the command prompt, the following creates an executable file:

g++ -o a.out main.cpp class1.cc class2.cc /usr/lib/libgsl.a /usr/lib/libgslcblas.a 

However, I'm not sure how to get cmake to work correctly. When I add a line like

 include_directories(/usr/lib/) link_libraries(usr/lib/libgsl.a usr/libgslcblas.a) 

the configuration seems to work, but the build fails:

 CMakeFiles/kmv.dir/main.o: In function `main': main.cpp:27: undefined reference to `gsl_matrix_alloc' main.cpp:35: undefined reference to `gsl_matrix_fscanf' collect2: ld returned 1 exit status make[2]: *** [kmv] Error 1 make[1]: *** [CMakeFiles/kmv.dir/all] Error 2 make: *** [all] Error 2 *** Failed *** 

This seems to be a syntax issue. Any hint is welcome. Thanks.

+4
source share
2 answers

Instead

 include_directories(/usr/lib) link_libraries(usr/lib/libgsl.a usr/libgslcblas.a) 

to try

 add_executable (targetName main.cpp class1.cc class2.cc) target_link_libraries(targetName gsl gslcblas) 

Where targetName is the name of the output binary you intend to create. The path /usr/lib should already be in the default search path for CMake, so you do not need to specify it, but if you need to specify the path to the user library, you will do it like this:

 link_directories(/some/custom/library/path) 

The CMake include_directories directive is used to add header search paths, not library search paths ...

+6
source

Link_libraries may be outdated
http://www.cmake.org/pipermail/cmake/2009-April/028439.html

Try using target_link_libraries instead.

+4
source

Source: https://habr.com/ru/post/1397903/


All Articles