0

I'm learning CMake at the moment and I'm stuck with a problem. I have a library(that I have created previously with CMake) named 'libLibraryName'(I know, not the best name). This library contains just one function that prints a message and was formed by combining two files(1 header file wtih the prototype of the function and one .cpp with it's implementation).

Now I put the function in a folder and I'm trying to make a .cpp file make use of this library. This is the structure of my folder(as you can see the library is in the .../Library folder).

.
├── build
├── CMakeLists.txt
├── include
│   └── header.h
├── Library
│   └── libLibraryName.so
├── main.cpp
└── src
    └── header.cpp

I wrote this CMakeLists:

cmake_minimum_required(VERSION 3.5.1)

# Project name
project(Project_Name)

# Include the existing library's name and then link it's path
# Make sure to provide a valid library name
set( PROJECT_LINK_LIBS libLibraryName.so )
link_directories(/home/uidr0938/CMake_Exercises/Library)

# Add path of the header files
include_directories(include)

# Create an executable with your main application's code
add_executable(Application main.cpp)

# And link the library to that application
target_link_libraries(Application ${PROJECT_LINK_LIBS})

Then I went into build and ran 'cmake ..'. This worked. But then when I wrote 'make' I get this following error:

/usr/bin/ld: cannot find -lLibraryName
collect2: error: ld returned 1 exit status
CMakeFiles/Application.dir/build.make:94: recipe for target 'Application' failed
make[2]: *** [Application] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Application.dir/all' failed
make[1]: *** [CMakeFiles/Application.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

It is if the program is looking for the library at a different path not the one I provide it with the link_directories command.

Please help me solve this so I can use that library with my main function. Thank you.

I used a virtual linux machine for this example.

Kennedy
  • 277
  • 2
  • 7
  • 21
  • If you know full path to the library, pass it to the `target_link_libraries` call instead of splitting into dir and filename components. `set(PROJECT_LINK_LIBS "/home/uidr0938/CMake_Exercises/Library/libLibraryName.so")`. – Tsyvarev Jan 22 '18 at 15:56
  • Tsyvarev do you mean replace the first 'set' and the 'link_directories' with that? Or just replace my current set with that?(adding the library name at the end of path). I have tried the latter and got the same result. If I remove the set completely and use the path instead of the variable I get "No rule to make target error" – Kennedy Jan 22 '18 at 16:35
  • `If I remove the set completely and use the path instead of the variable I get "No rule to make target error"` - This means that the library file doesn't exist. Recheck its path. – Tsyvarev Jan 22 '18 at 16:38
  • Just check if the lib libLibraryName.so is located at /home/uidr0938/CMake_Exercises/Library via ls /home/uidr0938/CMake_Exercises/Library . I guess you want to link a lib which is not there because you first need to generate it. – KimKulling Jan 23 '18 at 08:03
  • @KimKulling I have generated it with a previous CMake script. Then I tried to use it in a different project which has nothing to do with the previous one(the one that created it). Thank you all for the answers – Kennedy Jan 23 '18 at 08:05
  • So it is placed in this folder? – KimKulling Jan 23 '18 at 08:49
  • And once more: [Do not use `link_directories` like this in CMake](https://stackoverflow.com/a/31471772/577603). – ComicSansMS Jan 23 '18 at 09:04

0 Answers0