jeudi 25 février 2021

Create a Shared library and use it in another project

I have a c++ project with following structure:

.
├── include
│   ├── common.h
│   ├── os.h
│   ├── helper.h
│
├── lib
|   ├── libcrypto.so
│   ├── libssl.so
|   ├── ...
|
└── src
    ├── common.c
    ├── helper.c

Now I want to make a shared or any kind of library of this whole project so that I can include helper.h header in some other c++ project totally independent of this one and call some function declared in this header file.

I tried making a shared library with the following CMakeLists file:

cmake_minimum_required(VERSION 3.0)

project(helperlib)

link_directories(lib)

file(GLOB SOURCES src/*.c)

include_directories(include)

add_library(${PROJECT_NAME} SHARED ${SOURCES})

install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})

file(GLOB HEADERS include/*.h)
install(FILES ${HEADERS} DESTINATION include/${PROJECT_NAME})

But when I use the libhelperlib.so file generated by cmake in another project and use #include "helper.h", it gives me error. Can anyone suggest how to do this. Thanks!

EDIT: This is the structure of the other project:

.
├── include
│   ├── constants.h
│   ├── datetime.h
│   ├── 
│
├── lib
|   ├── libhelperlib.so
│   ├── libboost_system.so
|   ├── liboost_system.a
|
└── src
    ├── constants.c
    ├── datetime.c
    ├── main.c

And CMakeLists.txt that I'm using for it is:

cmake_minimum_required(VERSION 3.0)

project(testProj)

include_directories(include)
link_directories(lib)

file(GLOB SOURCES src/*.c)
file(GLOB HEADERS include/*.h)

add_executable(testProj ${SOURCES} ${HEADERS})
target_link_libraries(testProj crypto helperlib boost_system)

And when I'm using #include "helper.h" in main .c it gives following error:

fatal error: helper.h: No such file or directory

Aucun commentaire:

Enregistrer un commentaire