Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 240
Sankar Avatar asked Jul 02 '12 12:07

Sankar


People also ask

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What does CMake Add_library do?

Adds a library target called <name> to be built from the source files listed in the command invocation. The <name> corresponds to the logical target name and must be globally unique within a project. The actual file name of the library built is constructed based on conventions of the native platform (such as lib<name>.


1 Answers

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})
like image 65
user2478081 Avatar answered Sep 24 '22 07:09

user2478081