cmake导入so库,克利翁& CMake。如何添加库(* .so)

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

#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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值