Assistance Needed for Creating a Shared Library with CMake

I need some guidance on creating a shared library using CMake. My C++ project is already configured in Visual Studio, with the target set to generate a DLL library. I now need to create a shared library using the same C++ project.

I’ve set up most of the configuration, but I’m unsure how to properly add or link the external dependencies required for compilation and achieving the desired output. Any advice or examples would be greatly appreciated.

Thanks in advance!

Umer Fiaz.

guidance on creating a shared library using CMake

CMake Tutorial is a good start. As for the “shared” part, given the lack of details, that would be just adding the SHARED type.

I’ve set up most of the configuration

It would be useful to see what you have done so far (and some more details about the “desired output”).

Thanks for your reply. I have created a script named “CMakeLists.txt” in the same directory as the other project files. In this script, I added links to external dependencies to the best of my knowledge. However, when I attempt to build the code to generate a .so file, I encounter the following error originating from the C++ header file see (attached screenshots below).

Text is better to be shared as text, not as pictures of text :slight_smile:

I have created a script named CMakeLists.txt

Like I said, it would be useful to take a look at its contents to see what you have done so far.
From the error message it seems that at least one headers path is missing from the project.

Thanks again for replying. Here is my script.

cmake_minimum_required(VERSION 3.10)
project(TUBSsalt_permd)

Set the C++ standard

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

Add the source files

set(SOURCES
modelTUBSsalt_permd.cpp
)

Add the header files

set(HEADERS
modelTUBSsalt_permd.h
)

Find GSL package

find_package(GSL REQUIRED)

Include directories for dependencies

include_directories(${CMAKE_SOURCE_DIR}/packages/gsl-msvc14-x64.2.3.0.2779/build/native/gsl)
include_directories(${CMAKE_SOURCE_DIR}/packages/gsl-msvc14-x64.2.3.0.2779/build/static)
include_directories(${GSL_INCLUDE_DIRS})

Locate the GSL library

find_library(GSL_LIB gsl HINTS /usr/lib /usr/local/lib ${CMAKE_SOURCE_DIR}/packages/gsl-msvc14-x64.2.3.0.2779/build/native)
find_library(GSL_CBLAS_LIB gslcblas HINTS /usr/lib /usr/local/lib ${CMAKE_SOURCE_DIR}/packages/gsl-msvc14-x64.2.3.0.2779/build/native)

Create the shared library

add_library(TUBSsalt_permd SHARED ${SOURCES})

Link the GSL libraries (only if they are found)

if (GSL_LIB AND GSL_CBLAS_LIB)
target_link_libraries(TUBSsalt_permd PRIVATE ${GSL_LIB} ${GSL_CBLAS_LIB})
else()
message(FATAL_ERROR “GSL or GSL CBLAS library not found.”)
endif()

That would look much better if you put it into a code block.

Okay, from what I see in the GSL package (is that the one?), it does not contain the conmodel.h file. So apparently this header is from a different package/library, which you don’t seem to be linking to. Do you even have this file on your disk, where is it located?

Yes, I have this file in the same folder, and the file name is “modelTUBSsalt_permd.h.” This file contains C++ code in which there is a command

#pragma once
#include “conmodel.h”
#include “convert.h”

namespace models {
class ModelTUBSsalt_permd: public ConstitutiveModel {}

The goal is to compile the code and create a “.so”" file.

That would also look better in a code block than just in bold.

And I was asking about conmodel.h file.

Yes, if I open my project in Visual Studio, this file is present in the external dependencies folder.

What is the path to that “external dependencies folder”, who put it there? And what Visual Studio has to do with this, will the file disappear if you don’t open your project in Visual Studio?

It’s there because of the configuration of the project in Visual Studio using GSL and all other required dependencies. The aim of configuration was to create a dll library using VS code.

You don’t seem to be understanding me. The error on your screenshot of a text says that compiler couldn’t find that file, which means that it isn’t added to the project include paths. So what is the path to that conmodel.h file on your disk (and as a bonus question, who put it there)?

Thanks a lot. I understand your question, but it’s a bit complex for a brief explanation. If it’s alright with you, I can reach out via email.