Clion& CMake的。如何添加库(* .so)

时间:2015-11-10 21:16:19

标签: c++11 cmake shared-libraries clion

我尝试在Clion IDE中编写代码(c / c ++)。我需要在项目中添加一些共享库。在这一刻,我想只运行程序(只有主要功能),它将能够添加我的外部库libAPIenergy.so的任何功能。我从这个论坛尝试了一些解决方案,但任何人都没有帮助。

下面我将介绍最少出错的解决方案。

主要功能

包括

#include "APIenergy.h"

CMake文件

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})

和错误:

/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

我还使用共享库添加到系统PATH LD_LIBRARY_PATH direcytory

export LD_LIBRARY_PATH=$HOME/Lib/Linux/x86

即可。:: EDIT ::。 1

在你的消化后

我的 CMake

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})

来自CMake的错误

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

还有一件重要的事情。我将 APIenergy.h 文件部署到带有项目的主目录。

。::编辑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:Cannot specify link libraries for target "TestProject" which is not built by this project.

。::编辑3 ::。

现在CMake文件没有错误

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)

只是我尝试过午餐的代码

#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;
}

编译器错误

/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 个答案:

答案 0 :(得分:7)

如果要从源文件创建自己的库,请使用add_library

如果您指定目标需要链接到其他人的库,请使用target_link_libraries