Sankar
Sankar

Reputation: 6522

cmake create a shared object

I have three files:

lib.c lib.h => They should be built as a .so file
client.c => This should be built as an executable.
Inside the client.c I include the lib.h file so as to get the declarations of the functions defined under lib.c

Can someone tell me the exact CMakeLists file that I should be using so that the source area is uncluttered with Cmake's temporary files and the binaries and the libraries (.dlls in case of windows I believe) generated in separate build and binary directories ?

Upvotes: 15

Views: 33746

Answers (2)

duncan
duncan

Reputation: 6283

Cmake builds on separate build directories by default (I did not test this example):

PROJECT(myproject)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

SET(mylibSRCS lib.c)
SET(myprogSRCS client.c)

ADD_LIBRARY(mylib ${mylibSRCS})
ADD_EXECUTABLE(myprog ${myprogSRCS})

TARGET_LINK_LIBRARIES(myprog mylib)

You do:

mkdir build
cd build
cmake ..
make

Everything would be under build.

Update: As mentioned by @chryss below, if you want the .so file to be generated, the command should be:

ADD_LIBRARY(mylib SHARED ${mylibSRCS})

Upvotes: 14

user2478081
user2478081

Reputation: 171

This solution will not create a .so file, but a cmake equivalent for further inclusion with cmake.
I am searching for a solution with will provide the equivalent to this:

g++ -shared -Wl,-soname,plugin_lib.so.1 -o plugin_lib.so plugin_lib.o

Which will generate a plugin_lib.so that can be loaded dynamically with dlopen at runtime.

The solution is missing the "SHARED" option like so:

ADD_LIBRARY(mylib SHARED ${mylibSRCS})

Upvotes: 17

Related Questions