0

I'm having some problems try to link two shared library to my main.cpp file in a cmake file.

My folder tree is:

**mainfolder**
    |_main.cpp
    |_CMakeLists.txt
    |
    | **lib**
         |_**lib1**
              |_CMakeLists.txt
              |_liblib1.so
              |_**src**
                   |_lib1.cpp
              |_**include**
                   |_lib1.h
         |_**lib2**
              |_CMakeLists.txt
              |_liblib2.so
              |_**src**
                   |_lib2.cpp
              |_**include**
                   |_lib2.h

the two CMakeLists.txt for the two libraries are quite similar and created according to this link:

cmake_minimum_required(VERSION 2.6)
project( LibraryLib1 C CXX )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
set(CMAKE_BUILD_TYPE Release)
find_package( OpenCV REQUIRED )
# Find source files
file(GLOB SOURCES src/*.cpp)
# Include header files
include_directories(include)
# Create shared library
add_library(${PROJECT_NAME} SHARED ${SOURCES})
# Install library
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
# Install library headers
file(GLOB HEADERS include/*.h)
install(FILES ${HEADERS} DESTINATION include/${PROJECT_NAME})

my main.cpp file is:

#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include "lib/lib1/lib1.h"
#include "lib/lib2/lib2.h"

using namespace cv;

int main()
{
    printf("Executing main.cpp");

    lib1 lib1object;
    lib2 lib2object;

    for(;;)
    {

         lib1object.Analize(param1, param2);
         lib2object.Draw(param1, param2, param3);
    }
    return 0;
}

My main.cpp should call openCV + the two libraries. Could you please tell me which line I have to add to the CMakeLists.txt in the main folder to run my main?

in this moment the mainfolder/CMakeLists.txt is:

cmake_minimum_required(VERSION 2.8)
project( MyProject C CXX )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS})

I really appreciate your help. Thanks!!

One more thing... If I modify tghe last row of the mainfolder/CMakeLists.txt as:

target_link_libraries( main ${OpenCV_LIBS} LibraryLib1 LibraryLib2)

where LibraryLib1 and LibraryLib2 are the name of the two library project, I obtain:

/usr/bin/ld: unable to find -lLibraryLib1
/usr/bin/ld: unable to find -lLibraryLib2
Community
  • 1
  • 1
alexmark
  • 369
  • 4
  • 20
  • Possible duplicate of [create a shared library with cmake](http://stackoverflow.com/questions/17511496/create-a-shared-library-with-cmake) – languitar Mar 01 '17 at 12:40
  • Welcome to StackOverflow. Can you please add the error message you receive? It looks like you are missing `include_directories( ${OpenCV_INCLUDE_DIRS} )`. – Florian Mar 01 '17 at 12:42
  • the problem is not related to OpenCV...I just need to know how to include the directories of the two .so library, so my main file is able to find the two functions Analize and Draw – alexmark Mar 01 '17 at 13:35
  • @languitar the two libraries are created as suggested in that post – alexmark Mar 01 '17 at 14:12
  • Please provide a minimal working example (`CMakeLists.txt`) that reproduces the errors you are seeing with your own libraries. Hard to tell without that. – languitar Mar 01 '17 at 14:16
  • I just can't see in your current example, where the libraries are created. – languitar Mar 01 '17 at 14:17
  • ok I did it @languitar – alexmark Mar 01 '17 at 14:31

1 Answers1

1

You have separate projects for the libraries and the main file. Therefore, the main file doesn't know the target names from the other projects. The easiest solution (as everything is in a single source tree) is to have just a single projects.

Therefore, use add_subdirectory to build your libarires directly in the main project:

cmake_minimum_required(VERSION 2.8)
project( MyProject C CXX )
add_subidrectory(lib/lib1)
add_subidrectory(lib/lib2)
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} LibraryLib1 LibraryLib2)

This ensures that target names are known. This assumes that you have calls in the subfolders like add_library(LibraryLib1.... Otherwise, exchange the names appropriately.

languitar
  • 6,554
  • 2
  • 37
  • 62
  • Hi languitar, first of all thanks for your answer....it works properly..... My question now is: if I modify main.cpp, it recompiles all the libraries? beacuse I would like to speed up the process without recompiling files that were not modified (the libraries for example) – alexmark Mar 01 '17 at 16:07
  • No, CMake will handle this. – languitar Mar 01 '17 at 16:29
  • Could you please accept this answer if it works for you. – languitar Mar 01 '17 at 16:52
  • another question: if i want to compile the two libraries as static instead of dynamic, what I have to change? – alexmark Mar 02 '17 at 08:41