C++ Link two Shared Library to main.cpp

2019-09-12 05:46发布

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

1条回答
Evening l夕情丶
2楼-- · 2019-09-12 06:34

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.

查看更多
登录 后发表回答