克利翁& CMake。如何添加库(* .so) [英] Clion & CMake. How To Add Library (*.so)
问题描述
我正在尝试在Clion IDE中编写(c / c ++)代码。我需要在项目中添加一些共享库。目前,我只想运行main函数,它将添加我的外部库libAPIenergy.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.
主要函数我包括
#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
我还使用共享库将系统路径 LD_LIBRARY_PATH 添加到系统
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
我的 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 文件部署到了项目的主目录中。
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:Cannot specify link libraries for target "TestProject" which is not built by this project.
。:: EDIT 3 ::。
现在CMake文件没有错误
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;
}
编译器错误
/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
推荐答案
如果要从源文件创建自己的库,请使用 add_library
。
Use add_library
if you are creating your own library from source files.
如果您指定目标需要链接其他人的库,请使用 target_link_libraries
。
Use target_link_libraries
if you are specifying that your target needs to link against a library from someone else.
这篇关于克利翁& CMake。如何添加库(* .so)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!