Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Linux) Unable to link archives via cmake

Tags:

cmake

On the command line, the following produces an executable:

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

However I am unsure how to get cmake to work properly. When I add a line like

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

the configuring seems to work but building 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 ***

Seems to be a synthax problem. Any hint is welcome. Thank you.

like image 957
user974334 Avatar asked Dec 28 '22 05:12

user974334


2 Answers

Instead of

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

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 library search path for CMake, so you shouldn't have to specify that, but if you did have to specify a custom library path, you would do it like so

link_directories(/some/custom/library/path)

The include_directories CMake directive is used for adding header search paths, not library search paths...

like image 107
hatboyzero Avatar answered Jan 04 '23 03:01

hatboyzero


Probably, link_libraries is deprecated
http://www.cmake.org/pipermail/cmake/2009-April/028439.html

Try using target_link_libraries instead.

like image 31
anishsane Avatar answered Jan 04 '23 04:01

anishsane