Clion & CMake. How To Add Library (*.so)

2020-04-08 11:50发布

I trying write code (c/c++) in Clion IDE. I needs add to my project some shared library. In this moment I want to run just simply program (only main function) which will be able add any function witch my external library libAPIenergy.so. I tryed a few solutions from this forum but anyone nothing help.

Below I will present solution which give me 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

1条回答
爷的心禁止访问
2楼-- · 2020-04-08 12:25

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.

查看更多
登录 后发表回答