# Assistance Needed for Creating a Shared Library with CMake

**URL:** <https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483>\
**Category:** Code\
**Tags:** os:linux, os:windows, gen:makefiles\
**Created:** [August 19, 2024, 1:53pm UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483 "2024-08-19T13:53:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Umer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/aca169/32.png) [@Umer](https://discourse.cmake.org/u/Umer)\
**Post date:** [August 19, 2024, 1:53pm UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/1 "2024-08-19T13:53:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [August 19, 2024, 2:14pm UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/2 "2024-08-19T14:14:19Z")

</div>

> guidance on creating a shared library using CMake

[CMake Tutorial](https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20a%20Library.html#exercise-1-creating-a-library) 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”_).

---

<div class="post-metadata">

**Author:** ![Umer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/aca169/32.png) [@Umer](https://discourse.cmake.org/u/Umer)\
**Post date:** [August 19, 2024, 2:23pm UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/3 "2024-08-19T14:23:41Z")

</div>

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).

 ![Error](https://discourse.cmake.org/uploads/default/original/2X/3/3e1cb644b53eb5a930888a72f1a41af0af8a0659.png)

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [August 19, 2024, 2:36pm UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/4 "2024-08-19T14:36:20Z")

</div>

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

> 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.

---

<div class="post-metadata">

**Author:** ![Umer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/aca169/32.png) [@Umer](https://discourse.cmake.org/u/Umer)\
**Post date:** [August 20, 2024, 5:33am UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/5 "2024-08-20T05:33:50Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [August 20, 2024, 9:15am UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/6 "2024-08-20T09:15:48Z")

</div>

That would look much better if you put it into a [code block](https://meta.discourse.org/t/formatting-posts-using-markdown-bbcode-and-html/239348#supported-markdown-4).

Okay, from what I see in the [GSL package](https://www.gnu.org/software/gsl/) (_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?

---

<div class="post-metadata">

**Author:** ![Umer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/aca169/32.png) [@Umer](https://discourse.cmake.org/u/Umer)\
**Post date:** [August 20, 2024, 9:32am UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/7 "2024-08-20T09:32:20Z")

</div>

> [@Umer](#):
>
> modelTUBSsalt\_permd.h

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.

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [August 20, 2024, 9:33am UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/8 "2024-08-20T09:33:27Z")

</div>

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

And I was asking about `conmodel.h` file.

---

<div class="post-metadata">

**Author:** ![Umer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/aca169/32.png) [@Umer](https://discourse.cmake.org/u/Umer)\
**Post date:** [August 20, 2024, 9:39am UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/9 "2024-08-20T09:39:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [August 20, 2024, 9:40am UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/10 "2024-08-20T09:40:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Umer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/aca169/32.png) [@Umer](https://discourse.cmake.org/u/Umer)\
**Post date:** [August 20, 2024, 9:44am UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/11 "2024-08-20T09:44:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [August 20, 2024, 9:47am UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/12 "2024-08-20T09:47:30Z")

</div>

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_)?

---

<div class="post-metadata">

**Author:** ![Umer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/u/aca169/32.png) [@Umer](https://discourse.cmake.org/u/Umer)\
**Post date:** [August 20, 2024, 9:53am UTC](https://discourse.cmake.org/t/assistance-needed-for-creating-a-shared-library-with-cmake/11483/13 "2024-08-20T09:53:17Z")

</div>

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.
