Page 1 of 1

Can not compile executable with dynamic library

Posted: Thu Nov 22, 2018 1:19 am
by marlac
OS: Linux Mint 18.3 Cinamon 64bit ver. 3.6.7
CodeLite ver 12.0.0 - It is NOT self compiled version
--version gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609)

In workspace I have two projects :
1)Czujniki - which is executable gcc project with cmake
2)tinyxml - which is a dynamic library

After compiling tinyxml everthing is ok and in output directory I can find "libtinyxml.so".
However when I try compiling Czujniki I receive linker error

Code: Select all

/bin/sh -c 'cd /home/marek/Dokumenty/Czujniki/cmake-build-Debug/Czujniki && /usr/bin/make -e '
[ 33%] Linking CXX executable /home/marek/Dokumenty/Czujniki/cmake-build-Debug/output/Czujniki
/usr/bin/ld: cannot find -ltinyxml
collect2: error: ld returned 1 exit status
CMakeFiles/Czujniki.dir/build.make:120: recipe for target '/home/marek/Dokumenty/Czujniki/cmake-build-Debug/output/Czujniki' failed
make[2]: *** [/home/marek/Dokumenty/Czujniki/cmake-build-Debug/output/Czujniki] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Czujniki.dir/all' failed
make[1]: *** [CMakeFiles/Czujniki.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
====1 errors, 0 warnings====
In project Czujniki I have following settings :
Project Settings->Global Setings -> library path = ".;/output"
Project Settings->Global Setings -> Libraries = "libtinyxml.so"

How make linking working ?

Re: Can not compile executable with dynamic library

Posted: Thu Nov 22, 2018 1:39 am
by neildarlow
Try adding -L/path/to/libtinyxml.so to the linker flags to inform it where the library resides. The -ltinyxml tells the linker the name of the library only.

The Libraries search path of the Linker Settings in the Project Settings is probably a better way to do this.

Re: Can not compile executable with dynamic library

Posted: Fri Nov 23, 2018 12:57 am
by marlac
neildarlow wrote:Try adding -L/path/to/libtinyxml.so to the linker flags to inform it where the library resides. The -ltinyxml tells the linker the name of the library only.

The Libraries search path of the Linker Settings in the Project Settings is probably a better way to do this.
still doesn't work. What I did :
1) Clear whole output directory (/home/marek/Dokumenty/Czujniki/cmake-build-Debug)
2) in project Czujniki I cleared all items in: Settings->Global Settings
3) in project Czujniki I added :
Settings->Linker Options: -L/home/marek/Dokumenty/Czujniki/cmake-build-Debug/output/libtinyxml.so
Settings->Libraries : libtinyxml.so

4) Run CMake for tinyxml project
5) Run CMake for Czujniki project
6) Build tinyxml project -> output is libtinyxml.so in directory /home/marek/Dokumenty/Czujniki/cmake-build-Debug/output/
7) Build Czujnik project-> linking problem

Code: Select all

/bin/sh -c 'cd /home/marek/Dokumenty/Czujniki/cmake-build-Debug/Czujniki && /usr/bin/make -e '
Scanning dependencies of target Czujniki
[ 33%] Building CXX object CMakeFiles/Czujniki.dir/config.cpp.o
[ 66%] Building C object CMakeFiles/Czujniki.dir/main.c.o
[100%] Linking CXX executable /home/marek/Dokumenty/Czujniki/cmake-build-Debug/output/Czujniki
/usr/bin/ld: cannot find -ltinyxml
collect2: error: ld returned 1 exit status
CMakeFiles/Czujniki.dir/build.make:120: recipe for target '/home/marek/Dokumenty/Czujniki/cmake-build-Debug/output/Czujniki' failed
make[2]: *** [/home/marek/Dokumenty/Czujniki/cmake-build-Debug/output/Czujniki] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Czujniki.dir/all' failed
make[1]: *** [CMakeFiles/Czujniki.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
====1 errors, 0 warnings====
I wonder why linker wants to link "/usr/bin/ld: -ltinyxml" instead of /usr/bin/ld: -llibtinyxml.
I didn't set tinyxml library name in Czujnki project. But I set libtinyxml in linker section in Czujniki project. How to solve it ?

Re: Can not compile executable with dynamic library

Posted: Fri Nov 23, 2018 5:10 pm
by neildarlow
For your library project you should need to make no changes. It should build and put the library in cmake-build-Debug/output.

For your executable project you need the following settings:

Code: Select all

Compiler -> Include Paths: ../library_project_dir
Linker -> Libraries Search Path: ./cmake-Build-$(WorkspaceConfiguration)/output
Linker -> Libraries: library_name
You might also want to set a build order so that your library is built before the executable. This is done from right-click on the executable project and click on Build Order... then selecting the checkbox next to the library name.

I believe your problem was in specifying your paths. The Libraries Search Path is relative to the workspace and translates to the -L option.

The Libraries setting should be the basic library name (no lib prefix or .so suffix) and translates to the -l option.

Remember to export CMakeLists.txt and run CMake for the project after making any of these changes before attempting to build the executable.

EDIT: This solution is impacted by github issue #2185 where $(WorkspaceConfiguration) is passed. unchanged, into CMakeLists.txt. A workaround is to use ${CONFIGURATION_NAME} instead.