Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clion & CMake. How To Add Library (*.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 <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
like image 749
Mbded Avatar asked Nov 10 '15 21:11

Mbded


People also ask

Is CLion free for students?

CLion is a commercial product built on our own open-source IntelliJ Platform. As all other JetBrains products, CLion has a variety of licensing options, including free and paid ones. Students and open source projects qualify for free licenses. CLion is also available as a part of All Products pack.

Is CLion a good IDE?

CLion is the best IDE for developing C and C++ applications. Using this IDE we save most of the development time. Debugging in this IDE is super easy. Throughout our final year project we use CLion.

Which is better CLion or Vscode?

Visual Studio Code has a broader approval, being mentioned in 1134 company stacks & 2379 developers stacks; compared to CLion, which is listed in 9 company stacks and 8 developer stacks.

What is CLion used for?

CLion is a smart IDE for developing in C or C++ on Windows, Linux, and macOS. It's packed with an in-depth code analyzer, a range of code generation options, and the ability to navigate to anywhere in your code in one click.


1 Answers

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.

like image 172
legalize Avatar answered Sep 19 '22 21:09

legalize