1

I'm trying to include shared library in my main using CMake.

I used this example Trying to build shared library in CLion/CMake and C++ Link two Shared Library to main.cpp but its not working. I am getting undefined reference to all the function call.

My directory tree is like that:

**folder**
| **project folder**
|      | 
|      |_main.cpp
       |_
|      |_CMakeLists.txt
|      |
| **lib**
     |_**lib1**
          |_CMakeLists.txt
          |_liblib1.so
          |_**src**
               |_lib1.cpp
          |_**include**
               |_lib1.h      

This is my CMakeLists.txt of the project:

cmake_minimum_required(VERSION 3.14)
project(DAL_project)

set(CMAKE_CXX_STANDARD 17)
set(GCC_COVERAGE_COMPILE_FLAGS "-lpq")
set(GCC_COVERAGE_LINK_FLAGS    "-lpq")

include_directories(/usr/include)
include_directories(-I/home/yaodav/Desktop/git_repo/lib/internal/conn2/include/)
SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
SET(SOURCE_FILES main.cpp  SymbolInfo.cpp SymbolInfo.h SymbolDAL.cpp SymbolDAL.h AppSettingDAL.cpp AppSettingDAL.h AppSetting.cpp AppSetting.h)
add_executable(DAL_project ${SOURCE_FILES} )

I am trying to add those lines to the CMakeLists.txt but it is not working:

target_link_libraries(DAL_project conlibcon.so)
add_library(libconnlib SHARED IMPORTED -L../lib/internal/conn1)
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
yaodav
  • 1,126
  • 12
  • 34
  • Accepted answer is described in [that answer](https://stackoverflow.com/a/10550334/3440745) to the duplicate question. – Tsyvarev Jul 15 '19 at 08:59

1 Answers1

2
project(DAL_project)

set(CMAKE_CXX_STANDARD 17) 
set(GCC_COVERAGE_COMPILE_FLAGS "-lpq") 
set(GCC_COVERAGE_LINK_FLAGS "-lpq")

include_directories(/usr/include) 
include_directories(/home/yaodav/Desktop/git_repo/lib/internal/conn2/include) 
link_directories(/lib/internal/conn1 )
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}") 
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}") 
SET(SOURCE_FILES main.cpp SymbolInfo.cpp SymbolInfo.h SymbolDAL.cpp SymbolDAL.h AppSettingDAL.cpp AppSettingDAL.h AppSetting.cpp AppSetting.h) 
add_executable(DAL_project ${SOURCE_FILES} )
target_link_libraries(DAL_project connlib)

if this not work try to do this add_library( LIB_NAME SHARED IMPORTED) set_property(TARGET LIB_NAME PROPERTY IMPORTED_LOCATION FULL_PATH_TO_LIB)

Add the lib to target_link_libraries and the include files to the include_directories

alon
  • 220
  • 1
  • 16