1

guys, I am relatively new to CMake and now I have a bug that I don't understand. Actually I thought I had no problems understanding the terms static and shared library and the functions of CMake. But currently...

So in my project projB I wanna use a static_library from project projA. ProjA is already compiled, linked, installed and used on my board. For ProjB the linker cannot find the library "example".

My issue: for me, i doesn't make sense why projA can find the shared library, projB not. The error code:

Linking main ....
   ld: cannot find -lexample
   error: ld returned 1 exit status

My folder structure of projA:

include/ ... *.h (e. g. file.h)
src/ ... *.c (e. g. file.c)
CMakeFiles.txt

The CMakeLists.txt-File of projA:

cmake_minimum_required(VERSION 3.7)
PROJECT(projA)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
    
add_definitions(-g -O)
    
include_directories (include)
    
file (GLOB SOURCE_FILES src/*.c)
add_library (example STATIC ${SOURCE_FILES})
    
target_include_directories(example PUBLIC ${SOURCE_DIR}/include)
install(TARGETS example ARCHIVE DESTINATION usr/lib)

So projA will be later a library-component which can be used in many other projects. That's the reason why the created library is not part of projB and why I wanna import/include the created example.a file in a lot of other projects. The static library example.a should be stored in usr/lib.

In my projB the folder structure looks like:

   CMakeFiles.txt
   main.c
   .... *.c

In my main.c-File I try to include the library with #<file.h> which is a part of my created library example.

The CMakeFiles.text of ProjB containing following Code:

cmake_minimum_required(VERSION 3.7)

PROJECT(projB)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set (CMAKE_C_STANDARD   99)
set (CMAKE_CXX_STANDARD 14)

add_definitions(-g -O -fpermissive)

# build executable
file (GLOB SOURCES *.c )
add_executable (exe ${SOURCES})

# target_include_directories(exe PUBLIC ${CMAKE_SOURCE_DIR}/usr/lib)

# add_library(example SHARED IMPORTED)
# set_property(TARGET example PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/usr/libexample.a)

target_link_libraries (exe example pthread)

install (TARGETS exe RUNTIME DESTINATION usr/sbin)

An attempt to find the error is commented out. I also worked with find_library(), but the library "example" could not be found. Of course I also built the library example as a shared_library and searched for a .so, etc.

As you can see from my example, this is not real code (used in production), but a simplified description of my problem. I am more interested in the systematic. Wrong thinking?

Does anyone have any idea what else I can try or why it doesn't work?

Is it because they are different projects? I mean, the file exists on my system, but is not found ...

Thanks a lot for your help!

Greetings Matthias

doerflia
  • 65
  • 2
  • 5
  • 1
    Assuming you use default *installation prefix*, which is `/usr/local`, option `DESTINATION usr/lib` (with **relative** path) means the library will be installed under `/usr/local/usr/lib`. This is definitely not the path where the linker searches libraries by default. If you want to install library under `/usr/lib`, pass exactly this path (**absolute**) to `DESTINATION` option. If you want the linker to find the library in the custom location, see that question: https://stackoverflow.com/questions/8774593/cmake-link-to-external-library. – Tsyvarev Sep 18 '20 at 10:49

0 Answers0