🌐
GitHub
github.com › m3y54m › library-with-cmake
GitHub - m3y54m/library-with-cmake: Creating a shared or static library using cmake · GitHub
Creating a shared or static library using cmake. Contribute to m3y54m/library-with-cmake development by creating an account on GitHub.
Author   m3y54m
🌐
GitHub
github.com › ttroy50 › cmake-examples › blob › master › 01-basic › C-static-library › README.adoc
cmake-examples/01-basic/C-static-library/README.adoc at master · ttroy50/cmake-examples
This will be used to create a static library with the name libhello_library.a with the sources in the add_library call. ... As mentioned in the previous example, we pass the source files directly to the add_library call, as recommended for modern CMake.
Author   ttroy50
Discussions

Question: How to instruct cmake to generate a static library?
How to instruct cmake to generate a static library. Do I need to define any MACRO. I'm using VC17 + clang-cl-9. The generation goes fine, I just want/need a static library. Thanks in advance. More on github.com
🌐 github.com
39
March 4, 2019
c - CMake - Creating a static library - Stack Overflow
My project Test4 contains two files named: Structure.h Structure.c I am using this project to create a static library that can be used by other software projects. You can see how my CMake file is More on stackoverflow.com
🌐 stackoverflow.com
How do I tell CMake to link in a static library in the source directory? - Stack Overflow
I have a small project with a Makefile which I'm trying to convert to CMake, mostly just to get experience with CMake. For purposes of this example, the project contains a source file (C++, though ... More on stackoverflow.com
🌐 stackoverflow.com
CMake: why is there no easy way to build a static library that contains linked static libraries?
this is exactly what static linking is supposed to do, the 3rd party .a libs will be bundled within the library or executable that statically links them More on reddit.com
🌐 r/cpp_questions
19
17
October 13, 2022
🌐
GitHub
github.com › ttroy50 › cmake-examples › tree › master › 01-basic › C-static-library
cmake-examples/01-basic/C-static-library at master · ttroy50/cmake-examples
June 24, 2019 - Shows a hello world example which first creates and links a static library. This is a simplified example showing the library and binary in the same folder. Typically these would be in sub-projects as described in section 02-sub-projects ... $ tree . ├── CMakeLists.txt ├── include │ └── static │ └── Hello.h └── src ├── Hello.cpp └── main.cpp
Author   ttroy50
🌐
GitHub
github.com › bluescarni › mppp › issues › 175
Question: How to instruct cmake to generate a static library? · Issue #175 · bluescarni/mppp
March 4, 2019 - How to instruct cmake to generate a static library. Do I need to define any MACRO. I'm using VC17 + clang-cl-9. The generation goes fine, I just want/need a static library. Thanks in advance.
Author   degski
🌐
Alexreinking
alexreinking.com › blog › building-a-dual-shared-and-static-library-with-cmake.html
Building a Dual Shared and Static Library with CMake
March 6, 2021 - This also serves as a basic project template for a modern CMake library build. The main thing it's missing is handling dependencies. TLDR: See this GitHub repo with the full code, complete with GitHub Actions testing. So why is it tricky to provide both a static and shared version of a library ...
🌐
GitHub
github.com › topics › static-library
static-library · GitHub Topics · GitHub
A demonstration of how native projects can be created as static library and be part of a library which can then be linked up to the main application into a shared library. android cmake native cmakelists static-library shared-library
Top answer
1 of 4
74

The add_library line should be all you need. See this example code I just wrote to test out creating one and then using it (on Ubuntu 16.04):

Structure.h:

int sum( int a, int b );

Structure.c:

int sum( int a, int b ) { 
    return a + b;
}

Main.c:

#include <stdio.h>
#include "Structure.h"

int main() {
    int a = 5;
    int b = 8;
    int c = sum( a, b );

    printf( "sum of %d and %d is %d\n", a, b, c );

    return 0;
}

CMakeLists.txt:

# CMake instructions to make the static lib

ADD_LIBRARY( MyStaticLib STATIC
             Structure.c )


# CMake instructions to test using the static lib

SET( APP_EXE StaticTest )

ADD_EXECUTABLE( ${APP_EXE}
                Main.c ) 

TARGET_LINK_LIBRARIES( ${APP_EXE}
                       MyStaticLib )

And then here is the output from running it:

nick@dusseldorf:~/code/cmake/static_lib$ ls
CMakeLists.txt  Main.c  Structure.c  Structure.h

nick@dusseldorf:~/code/cmake/static_lib$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nick/code/cmake/static_lib

nick@dusseldorf:~/code/cmake/static_lib$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  Main.c  Makefile  Structure.c  Structure.h

nick@dusseldorf:~/code/cmake/static_lib$ make
Scanning dependencies of target MyStaticLib
[ 25%] Building C object CMakeFiles/MyStaticLib.dir/Structure.c.o
[ 50%] Linking C static library libMyStaticLib.a
[ 50%] Built target MyStaticLib
Scanning dependencies of target StaticTest
[ 75%] Building C object CMakeFiles/StaticTest.dir/Main.c.o
[100%] Linking C executable StaticTest
[100%] Built target StaticTest

nick@dusseldorf:~/code/cmake/static_lib$ ls
CMakeCache.txt  cmake_install.cmake  libMyStaticLib.a  Makefile    Structure.c
CMakeFiles      CMakeLists.txt       Main.c            StaticTest  Structure.h

nick@dusseldorf:~/code/cmake/static_lib$ ./StaticTest 
sum of 5 and 8 is 13
2 of 4
6

I had same issue. What I missed is the location where build files are created.

CLion makes libraries or exectables under cmake-build-* directory. IfBuild, Execution, Deployment > CMake > Configuration is Debug, the lib file (.a) is created under cmake-build-debug.

Find elsewhere
🌐
GitHub
github.com › pablospe › cmake-example-library
GitHub - pablospe/cmake-example-library: How to install a library with cmake
Autogenetared library version file: #include <foo/version.h> FOO_DEBUG added on Debug. See foo/foo.cpp#L7-L11. CMAKE_DEBUG_POSTFIX = 'd' (allowing Debug and Release to not collide). See cmake/SetEnv.cmake#L17. Static library as default (BUILD_SHARED_LIBS=OFF).
Starred by 157 users
Forked by 25 users
Languages   CMake 68.9% | Shell 28.3% | C++ 2.3% | C 0.5% | CMake 68.9% | Shell 28.3% | C++ 2.3% | C 0.5%
🌐
M-tmatma
m-tmatma.github.io › cmake › cmake-library.html
cmake での shared library/static library のテストプロジェクト
option (BUILD_SHARED_LIBS "Build Shared Libraries" ON) # Set the LIB_TYPE variable to STATIC set (LIB_TYPE STATIC) if (BUILD_SHARED_LIBS) # User wants to build Dynamic Libraries, so change the LIB_TYPE variable to CMake keyword 'SHARED' set (LIB_TYPE SHARED) endif (BUILD_SHARED_LIBS) BUILD_SHARED_LIBS が OFF になっていたら ${LIB_TYPE} は STATIC (Static Library) になります。 そして BUILD_SHARED_LIBS はコマンドラインで cmake を実行するときに上書きすることができます。 以下のように実行してプロジェクトを生成すれば Shared Library (DLL) になります。
🌐
Libigl
libigl.github.io › static-library
Static Library - libigl
The installation process does not only create ${CMAKE_INSTALL_PREFIX}/lib/libigl.a, it copies the libigl headers into ${CMAKE_INSTALL_PREFIX}/include/igl/ too. Or if you base your project on the libigl-example-project you can add · option(LIBIGL_USE_STATIC_LIBRARY "Use libIGL as static librarie" ON)
🌐
CMake
cmake.org › cmake › help › latest › command › add_library.html
add_library — CMake 4.3.0-rc3 Documentation
The optional <type> specifies the type of library to be created: ... A Static Library: an archive of object files for use when linking other targets.
🌐
GitHub
github.com › StMartin81 › cmake_library_example
GitHub - StMartin81/cmake_library_example: Example project which uses CMake to create a library
–-parallel 4 --config Release cmake --build . –-parallel 4 --config Release --target lib/install · The library can be build as static library when cmake is configured with -DBUILD_SHARED_LIBS=OFF.
Starred by 4 users
Forked by 2 users
Languages   CMake 66.0% | C++ 26.5% | C 7.5% | CMake 66.0% | C++ 26.5% | C 7.5%
🌐
GitHub
github.com › deech › libclang-static-build
GitHub - deech/libclang-static-build
This package consists of a set of CMake scripts that download and compile libclang into a single static archive containing all LLVM and third party dependencies so applications which link against it can be easily deployed. The build also produces a bundled shared library which allows faster iteration during development time by avoiding link time slowdowns.
Starred by 103 users
Forked by 13 users
Languages   CMake 93.3% | C 6.7% | CMake 93.3% | C 6.7%
🌐
Reddit
reddit.com › r/cpp_questions › cmake: why is there no easy way to build a static library that contains linked static libraries?
r/cpp_questions on Reddit: CMake: why is there no easy way to build a static library that contains linked static libraries?
October 13, 2022 -

AFAIK, CMake does not offer any (convenient) way to combine several static libraries into one (static or shared) library and doing so in a portable (generator, platform) way seems to be non-trivial. To me, this is kind of surprising, since it seems like a very common thing one might want to do. Of course, there are solutions ready for C&P on StackOverflow (e.g., [1]), but I'm kind of wondering: since this is such a hassle, does CMake even want me to do that or is there something wrong (non-idiomatic) with my setup?

My use case is as follows: I've got a project whose primary target is a single library libproject.{a, so}. The project does not depend on any large, well-known libraries; but it does use some smaller libraries to get a few things done. A user of my library should not have to worry about them; in fact, he shouldn't even (have to) know that I'm using those libraries to implement a few things. I might stop using them next month, or use even more libraries by then, and a user should not have to change his build instructions to also link against libsomewhatbettersamplinglibrary123.a because of that.

Since these libraries are all CMake projects and are on Github, I add them as Git submodules to my project, include them in my CMakeLists.txt and link them against my project using

target_link_libraries(project PRIVATE SomeDependencyA SomeDependencyB)

(The dependencies are defined using add_library(...) in their own CMakeLists.txt)

However, now my project compiles to a set of libraries, and a user of libproject.{a, so} also has to link against all those other libraries, which is exactly what I do not want.

Is this something that I'm not supposed to do? Not something that I'm supposed to want? Am I wrong in expecting that CMake should offer a convient way to "export" my project as a single library file that contains all of its (statically linked) dependencies? Or is there some other reason why this is such a hassle to achieve with CMake?

[1] https://stackoverflow.com/questions/11429055/cmake-how-create-a-single-shared-library-from-all-static-libraries-of-subprojec

🌐
Reddit
reddit.com › r/cpp › building a dual shared and static library with cmake
r/cpp on Reddit: Building a Dual Shared and Static Library with CMake
March 8, 2021 - I should have mentioned it in the article (in fact, you are right that it is important, so I will update it later today), but you just have to set CMAKE_<CONFIG>_POSTFIX on the static library build and everything just works. You can see that everything works on the GitHub Actions CI on all three of Windows, macOS, and Linux.
🌐
CGold
cgold.readthedocs.io › en › latest › tutorials › libraries › static-shared.html
3.11.3. Static + shared — CGold 0.1 documentation
Command add_library should be used without STATIC or SHARED specifier, type of the library will be determined by value of BUILD_SHARED_LIBS variable (default type is static): cmake_minimum_required(VERSION 3.4) project(foo) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES CACHE BOOL "Export all symbols") add_library(foo foo.cpp) install( TARGETS foo LIBRARY DESTINATION lib ARCHIVE DESTINATION lib RUNTIME DESTINATION bin )