0

I am linking a 3rd party library with my executable. There are buch of other libraries which I have successfully linked against. I'm not able understand what I am doing wrong.

This is the piece which is causing the issue

# Build libssh 0.7.3
ExternalProject_Add(libssh2
        URL ${PROJECT_SOURCE_DIR}/packages/libssh.tar.gz
        PREFIX ${CMAKE_BINARY_DIR}/external/libssh2
        CONFIGURE_COMMAND ""
        UPDATE_COMMAND ""
        BUILD_COMMAND mkdir -p <SOURCE_DIR>/build && cd <SOURCE_DIR>/build &&
            cmake -DWITH_STATIC_LIB=1 -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR> -DCMAKE_BUILD_TYPE=Debug .. && make
        INSTALL_COMMAND cd <SOURCE_DIR>/build && make install PREFIX=<INSTALL_DIR>
)

ExternalProject_Get_Property(libssh2 install_dir)
add_library(libssh STATIC IMPORTED)
set_property(TARGET libssh PROPERTY IMPORTED_LOCATION
        ${install_dir}/lib/libssh.a)
add_dependencies(libssh libssh2)
include_directories(${install_dir}/include)

This is another 3rd party which is linking correctly

ExternalProject_Add(libevent2
        URL ${PROJECT_SOURCE_DIR}/packages/libevent-2.0.21-stable.tar.gz
        PREFIX ${CMAKE_BINARY_DIR}/external/libevent2
        CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
        UPDATE_COMMAND ""
)
ExternalProject_Get_Property(libevent2 install_dir)
add_library(libevent STATIC IMPORTED)
set_property(TARGET libevent PROPERTY IMPORTED_LOCATION
        ${install_dir}/lib/libevent.a)
add_dependencies(libevent libevent2)
include_directories(${install_dir}/include)

Error which I am seeing

Linking C executable ../../bin/nixia
CMakeFiles/nixia.dir/main_config.c.o: In function `main_config_dump':
/home/nikhil/nixia/src/main/main_config.c:88: undefined reference to `ssh_new'
/home/nikhil/nixia/src/main/main_config.c:91: undefined reference to `ssh_options_set'
collect2: ld returned 1 exit status

The library is created correctly

[nikhil@nikhil lib]$ ls
cmake  libssh.a  libssh.so  libssh.so.4  libssh.so.4.4.1  libssh_threads.a  libssh_threads.so  libssh_threads.so.4  libssh_threads.so.4.4.1  pkgconfig
[nikhil@nikhil lib]$ pwd
/home/nikhil/nixia/build/external/libssh2/lib

The library contains the function definition

[nikhil@nikhil lib]$ ar x libssh.a | nm * | grep ssh_new
nm: Warning: 'cmake' is not an ordinary file
                 U ssh_new
0000000000000000 T ssh_new
00000000000351b0 T ssh_new
00000000000351b0 T ssh_new
00000000000351b0 T ssh_new
Nikhil
  • 2,168
  • 6
  • 33
  • 51
  • Is your executable also linking against all the libraries? You have not listed that part. – ToniBig May 02 '16 at 17:29
  • Yes they are..add_library() does that – Nikhil May 02 '16 at 17:38
  • 1
    I think `target_link_libraries` is what you need. `add_library` just creates a target for a library, which cannot be executed. – ToniBig May 02 '16 at 17:41
  • Could this be related to [cmake: linking against STATIC IMPORTED library fails](http://stackoverflow.com/questions/36846818/cmake-linking-against-static-imported-library-fails)? Maybe your linker does find another `libssh.a` is you system, because CMake just puts an `-lssh`? Check with [make verbose output](http://stackoverflow.com/questions/2670121/using-cmake-with-gnu-make-how-can-i-see-the-exact-commands) what your command line looks like. And try to add an `GLOBAL` to your `add_library(libssh STATIC IMPORTED)` call. – Florian May 03 '16 at 18:58

1 Answers1

3

I suggest you to compile using CMake verbose mode in order to check the full command used to compile it.

In order to do so you have to compile using the following command:

make VERBOSE=1

In this manner you can check which is the library that was linked identifying the problem.

Possible reasons of the undefined reference error:

  • The library has not been found (check if -L and -l flags in the g++ command are correct and, in case of errors, modify the CMake to obtain the desired result);
  • On the same system there are more versions of the same library (in your case libssh): the compiler could use the header files of a version and the library of another which could have a different signature producing linking errors.
Francesco Argese
  • 626
  • 4
  • 11