c++cmakeclioncereal

C++ CMake compilation error (/usr/bin/ld: cannot find <LIBRARY_NAME>)


*I know there are lots of questions about this, but they simply don't help much when talking about CMake, hence my decision of making the question *

So I was working on CLion which uses CMake in order to import and give parameters to the compiler, and successfully included (imported) an external library (cereal: to serialize classes into json files) located on a folder called "ExternalLibraries" which is on the root of my project folder. It was working just fine, untill I restarted the IDE and tried to ran the code again... It returned a compilation error (I think).

My CMake file looks like this:

cmake_minimum_required(VERSION 3.3)
project(xMemory)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

include_directories ("${PROJECT_SOURCE_DIR}/ExternalLibraries/cereal-1.1.2/include/")

set(SOURCE_FILES main.cpp xObject.cpp xObject.h)
add_executable(xMemory ${SOURCE_FILES})
target_link_libraries (xMemory cereal)

And when I try to run/compile the shell gives me this:

/home/lunaticsoul/Documents/clion-1.2.4/bin/cmake/bin/cmake --build /home/lunaticsoul/.CLion12/system/cmake/generated/95701c38/95701c38/Debug0 --target xMemory -- -j 4
Scanning dependencies of target xMemory
[ 33%] Building CXX object CMakeFiles/xMemory.dir/xObject.cpp.o
[ 66%] Building CXX object CMakeFiles/xMemory.dir/main.cpp.o
[100%] Linking CXX executable xMemory
/usr/bin/ld: cannot find -lcereal
collect2: error: ld returned 1 exit status
make[3]: *** [xMemory] Error 1
make[2]: *** [CMakeFiles/xMemory.dir/all] Error 2
make[1]: *** [CMakeFiles/xMemory.dir/rule] Error 2
make: *** [xMemory] Error 2

I'm not sure about what is happening cause the library seems to actually import into the code (There are no red letters when including cereal) and as I said before, I think it just stopped working.

Can someone tell me if there is something wrong with my CMake file?

PD: Here's a screenshot just in case anyone need it.

PD2: I'm using elementary os: Freya (Ubuntu 14.04)

enter image description here


Solution

  • it works if adding:

    target_link_libraries (xMemory /library_build_path/libcereal.a)
    

    details:

    ld is looking for the libraries in a very short list of folders defined in

    /etc/ld.so.conf
    

    and it usually looks like following:

    include /etc/ld.so.conf.d/*.conf
    

    and actual paths list from those *.conf files usually is like:

    # Legacy biarch compatibility support
    /lib32
    /usr/lib32
    # Multiarch support
    /usr/local/lib/x86_64-linux-gnu
    /lib/x86_64-linux-gnu
    /usr/lib/x86_64-linux-gnu
    

    if your project linking library is not in the folder of this list, ld won't find it unless either a special linking variable set LD_LIBRARY_PATH with the path to your library or a complete path/library name provided in cmake target_link_libraries directive.

    details on how to proper setup LD_LIBRARY_PATH variable discussed here