By using CMake to generate .SO shared object file

I am using our sample project to try to generate .SO file, the following are my Cmake file code, I don’t know how to generate the .SO file by using our CMake file.

cmake_minimum_required(VERSION 3.18)
project(runWalktrap)

#Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

find_package(igraph REQUIRED)

add_executable(runWalktrap runWalktrap.cpp)
target_link_libraries(runWalktrap PUBLIC igraph::igraph)

I tried the following method, but it doesn’t work.

gcc -shared runWalktrap.cpp.o -o runWalktrap.so

Do you guys have any idea about how to genreate .SO file by using our CMake file?

If you want to create a library instead of an executable, use add_library instead of add_executable.

That said, this question is not related to igraph in any way. For general programming related questions, I suggest StackOverflow. CMake also have their forum.

Thank you for your reply. Next time I will post the general programming related questions to Other forums. But for this one, it’s a little bit difference. Because when I try to create the .SO file, I will got an error, I only can create .a file.

cmake_minimum_required(VERSION 3.18)
project(runWalktrap)

#Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

find_package(igraph REQUIRED)

#add_executable(runWalktrap runWalktrap.cpp)
add_library(runWalktrap SHARED runWalktrap.cpp)
target_link_libraries(runWalktrap PUBLIC igraph::igraph)

The reason is when I installed our igraph library, it gerneated an libigraph.a file, which is an STATIC library object, if I want to generate a .SO file, I have to use DYNAMIC library to genreate that. But I don’t know how to do that. It seems like I need to generate an libigraph.so file first, then I can use our Cmake file to generate a .SO file.

This is also a more general CMake related question. You probably want to read their documentation. If you don’t find an answer there, you can probably better ask for directions on their forum.

In this case, you should probably check out whether BUILD_SHARED_LIBS does what you want.

2 Likes

When compiling igraph, enable PIC

https://cmake.org/cmake/help/latest/variable/CMAKE_POSITION_INDEPENDENT_CODE.html

1 Like

This flag is on by default for shared libraries. It is only explicitly needed in more complicated cases I believe (e.g. static libraries linked into dynamic libraries).

1 Like

Thank you for your suggestion. Now I can gerneate the .SO file for no problem. The process is I need to go to the igraph-0.10.4/CmakeLists.txt to do the following change:

# Expose the BUILD_SHARED_LIBS option in the ccmake UI
#option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)

I need to switch the OFF tag to ON. And then I need to add the following Code to the target project’s Cmake.txt file:

add_library(XXX SHARED XXX.cpp)

After this, I can generate .SO file for no problem. The reason is if I want to generate .SO file, I need to generate the shared lib for iGraph first, and then using the igraph.so to generate my project .SO file.

You should not modify igraph’s CMake files. Set the options using standard means. Please read through the installation instructions:

https://igraph.org/c/html/latest/igraph-Installation.html

as well as through the CMake user docs:

https://cmake.org/cmake/help/latest/guide/user-interaction/index.html#setting-build-variables

In particular, read the Setting Build Variables section.

1 Like

Thank you for your reply. I will read these instructions and do change the igraph’s CMake files.