8

I am trying to write (c/c++) code in Clion IDE. I need to add some shared library into my project. at this moment, I only want to run the main function, which will add all functions which my external library libAPIenergy.so has. I tried a few solutions from this forum but none of them helped.

Below I will present solution which give me the least errors.

In main function I include

#include "APIenergy.h"

CMake file

cmake_minimum_required(VERSION 3.3)
project(TestProject)
add_library( libAPIenergy SHARED IMPORTED )
link_directories (/home/I/Lib/Linux/x86)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lAPIenergy ")
set(SOURCE_FILES main.cpp APIenergy.h)
add_executable(TestProject ${SOURCE_FILES})

And Errors:

/home/I/clion-1.2/bin/cmake/bin/cmake --build /home/I/.CLion12/system/cmake/generated/9faec492/9faec492/Debug --target TestProject -- -j 8
[ 50%] Building CXX object CMakeFiles/TestProject.dir/main.cpp.o
[100%] Linking CXX executable TestProject
/usr/bin/ld: cannot find -lAPIenergy
collect2: error: ld returned 1 exit status
CMakeFiles/TestProject.dir/build.make:94${PROJECT_SOURCE_DIR}/P2PTunnelAPIs.h.in": polecenia dla obiektu 'TestProject' nie powiodły się
make[3]: *** [TestProject] Błąd 1
CMakeFiles/Makefile2:67: polecenia dla obiektu 'CMakeFiles/TestProject.dir/all' nie powiodły się
make[2]: *** [CMakeFiles/TestProject.dir/all] Błąd 2
CMakeFiles/Makefile2:79: polecenia dla obiektu 'CMakeFiles/TestProject.dir/rule' nie powiodły się
make[1]: *** [CMakeFiles/TestProject.dir/rule] Błąd 2
Makefile:118: polecenia dla obiektu 'TestProject' nie powiodły się
make: *** [TestProject] Błąd 2

I also have added to system PATH LD_LIBRARY_PATH direcytory with my shared library

export LD_LIBRARY_PATH=$HOME/Lib/Linux/x86

.::EDIT::. 1

My CMake after Yours sugestion

cmake_minimum_required(VERSION 3.3)
project(TestProject)
add_library(libAPIenergy SHARED IMPORTED)
SET_PROPERTY(TARGET libAPIenergy PROPERTY IMPORTED_LOCATION /home/I/x86/libAPIenergy.so)
target_link_libraries(TestProject libAPIEnergy)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp APIenergy.h)
add_executable(TestProject ${SOURCE_FILES})

Error from CMake

Error:Cannot specify link libraries for target "TestProject" which is not built by this project.

And one important thing. I deployed APIenergy.h file to main directory with project.

.::EDIT 2::.

cmake_minimum_required(VERSION 3.3)
project(TestProject)
add_library(libAPIenergy SHARED IMPORTED)

target_link_libraries(TestProject libAPIenergy)
SET_PROPERTY(TARGET libAPIenergy PROPERTY IMPORTED_LOCATION /home/I/lib/x86/libAPIenergy.so)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp APIenergy.h)

add_executable(TestProject ${SOURCE_FILES})
target_link_libraries(TestProject libAPIenergy)

Error

Error:Cannot specify link libraries for target "TestProject" which is not built by this project.

.::EDIT 3::.

Now CMake file is without error

cmake_minimum_required(VERSION 3.3)
project(TestProject)
add_library(libAPIenergy SHARED IMPORTED)

SET_PROPERTY(TARGET libAPIenergy PROPERTY IMPORTED_LOCATION /home/I/lib/x86/libAPIenergy.so)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp APIenergy.h)

add_executable(TestProject ${SOURCE_FILES})
target_link_libraries(TestProject libAPIenergy)

And simply code which I have tryed lunch

#include <iostream>
#include "APIenergy.h" // include without error this mean without underlined

using namespace std;

int main() {

    int ret = APIenergyInitialize(5); // IDE suggestions name function so is looks good

    cout << "Hello, World!" << endl;
    return 0;
}

Error from compiler

/home/I/clion-1.2/bin/cmake/bin/cmake --build /home/I/.CLion12/system/cmake/generated/9faec492/9faec492/Debug0 --target TestProject -- -j 8
[ 50%] Linking CXX executable TestProject
/home/I/lib/x86/libAPIenergy.so: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
CMakeFiles/TestProject.dir/build.make:95: polecenia dla obiektu 'TestProject' nie powiodły się
make[3]: *** [TestProject] Błąd 1
CMakeFiles/Makefile2:67: polecenia dla obiektu 'CMakeFiles/TestProject.dir/all' nie powiodły się
make[2]: *** [CMakeFiles/TestProject.dir/all] Błąd 2
CMakeFiles/Makefile2:79: polecenia dla obiektu 'CMakeFiles/TestProject.dir/rule' nie powiodły się
make[1]: *** [CMakeFiles/TestProject.dir/rule] Błąd 2
Makefile:118: polecenia dla obiektu 'TestProject' nie powiodły się
make: *** [TestProject] Błąd 2
H.ramezani
  • 25
  • 8
Mbded
  • 1,754
  • 4
  • 23
  • 43
  • 1
    `CMAKE_CXX_FLAGS` are *compiler* flags, not a *linker* ones. For link executable with library, you need issue `target_link_libraries(TestProject libAPIenergy)` after `add_executable()` call. Also, for *imported* library you need to specify its location in `IMPORTED_LOCATION` property (this is noted in [documentation](https://cmake.org/cmake/help/v3.0/command/add_library.html) for `add_library`. – Tsyvarev Nov 10 '15 at 22:56
  • Please look for main post I edited him EDIT 1 – Mbded Nov 11 '15 at 07:48
  • 1
    `target_link_libraries()` call should come **after** `add_executable()`. – Tsyvarev Nov 11 '15 at 07:57
  • I eddited main post **EDIT 2**. Changed this line right to You sugestion but also give error form CMake Thank You for Your time. – Mbded Nov 11 '15 at 08:23
  • 1
    Please, check you code more carefully. Currently you have **two** `target_link_libraries()` calls, and one of them is located before `add_executable()`. – Tsyvarev Nov 11 '15 at 08:42
  • Yes You have a right sorry for that. I have corrected this. Now CMake looks good but compiler giving me error look for **EDIT 3** – Mbded Nov 11 '15 at 09:30
  • 1
    This error message tells you that your library file is incompatible with your current architecture. Usually this occures if you have built library *on another machine*. This is commonly faced problem, try to google it. – Tsyvarev Nov 11 '15 at 09:45
  • May be I knows what is happen. I Can build this porgram from `MakeFile` using flag `CFLAGS=-m32` and I also had install `libc6-dev-i386`. So may be I also in CMake need something set ? I have 64bit arch and library probably is build for 32 bit arch. I have tryed add `-m=32` but I get also this same error – Mbded Nov 11 '15 at 10:07
  • 1
    Try to add this option to `CMAKE_CXX_FLAGS` variable: `set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")` – Tsyvarev Nov 11 '15 at 10:26
  • I add new line `set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")` now I get error `[ 50%] Building CXX object CMakeFiles/TesProjectt.dir/main.cpp.o In file included from /home/I/Project/TestProject/main.cpp:1:0: /usr/include/c++/4.9/iostream:38:28: fatal error: bits/c++config.h: Nie ma takiego pliku ani katalogu #include ` Looks like after set this flag something is wrong in 'iostream' library. – Mbded Nov 11 '15 at 11:46
  • I Found solution this is some bug I must install `aptitude install g++-4.9-multilib` and evrything works fine. Thank You for help. Topic is solved – Mbded Nov 11 '15 at 12:34
  • Possible duplicate of [CMake link to external library](http://stackoverflow.com/questions/8774593/cmake-link-to-external-library) – usr1234567 Jan 28 '17 at 12:42

1 Answers1

10

Use add_library if you are creating your own library from source files.

Use target_link_libraries if you are specifying that your target needs to link against a library from someone else.

legalize
  • 2,214
  • 18
  • 25