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
Answer from ndeubert on Stack Overflow
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.

Discussions

Static library dependencies
So, let’s say I have separate libraries A, B & C in separate project folders. C is a static library but it’s CMake is unimportant for this example. B’s CMakeLists.txt looks something like this: add_library(B STATIC b.c… More on discourse.cmake.org
🌐 discourse.cmake.org
0
0
September 24, 2023
how to adding static library to cmakelists project
hello guys i am new to cmake, i used to use qmake, in my qmake i have the following for adding a static library that is inside a folder called bin, inside t... More on forum.qt.io
🌐 forum.qt.io
1
0
February 4, 2020
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
Building and linking external static lib to game with CMake
I have a library that I need to add to the game, and while I have it working using a VS project file for Win32 and Android.mk (LOCAL_STATIC_LIBRARIES += mylib_static) for Android, I’m not quite sure how to add it the the new CMake build process in Cocos2d-x 3.17. More on forum.cocosengine.org
🌐 forum.cocosengine.org
0
0
May 22, 2018
🌐
Reddit
reddit.com › r/cpp_questions › how can i include dependencies in a static library using cmake
r/cpp_questions on Reddit: How can I include dependencies in a static library using CMake
October 25, 2024 -

I'm working on a library to help me in creating new projects faster. The library is built on top of a few dependencies, like SDL2 and GLEW.

What I want to achieve is to only need to link againts my library and not its dependencies when creating new projects. I could do this when instead of using CMake, I edited the Librarian section of the VS project myself, but when I use CMake to generate the project files, the "Additional Dependencies" and "Additional Library Directories" sections of the project properties remain empty.

Some additional info:
- I'm using vcpkg to manage dependencies
- to link the libraries I use: target_link_libraries(${PROJECT_NAME} PUBLIC SDL2::SDL2 SDL2::SDL2main GLEW::GLEW)
- when I create an executable with add_executable and not a library with add_library everything works as expected.

I've just began learning about CMake a few days ago, so feel free to correct me if I'm doing something wrong.

🌐
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
🌐
CMake
cmake.org › cmake › help › latest › guide › tutorial › In-Depth CMake Library Concepts.html
Step 5: In-Depth CMake Library Concepts — CMake 4.3.0-rc3 Documentation
As we learned in Step1, the add_library() command accepts the name of the library target to be created as its first argument. The second argument is an optional <type> for which the following values are valid: ... A Static Library: an archive of object files for use when linking other targets.
🌐
CMake Discourse
discourse.cmake.org › code
Static library dependencies - Code - CMake Discourse
September 24, 2023 - So, let’s say I have separate libraries A, B & C in separate project folders. C is a static library but it’s CMake is unimportant for this example. B’s CMakeLists.txt looks something like this: add_library(B STATIC b.cpp) target_link_libraries(B PRIVATE C) And A’s CMakeLists.txt looks something like this: add_library(A STATIC a.cpp) target_link_libraries(A PRIVATE B) Now, I know the dependency to C is passed along to A thru B – that makes sense!
Find elsewhere
🌐
CMake
cmake.cmake.narkive.com › Bm8RKIrl › using-to-build-a-static-library
[CMake] Using cmake to build a static library
Well, static libraries just don't give you that. They are essentially glorified archive files (think "zip-file") containing the object files. When you do a TARGET_LINK_LIBRARIES(myStaticLib someOtherLib), CMake remembers that dependency internally and will add someOtherLib to all the link-lines ...
🌐
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

🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › cmake-static-lib-727825
CMake Static Lib
Hi I've got a question on cmake on which i have not found an answer for in the docs so far. I have a program A, a static library B, and an external
🌐
Kernel Panic
kerpanic.wordpress.com › 2017 › 08 › 22 › compiling-static-vs-dynamic-libraries-on-cmake
Compiling Static vs Dynamic Libraries on CMake – Kernel Panic
August 24, 2017 - It is interpreted as a dynamic linked library. ... Tells CMake to look for this static library file in the linked directories, and build it statically into your binary.
🌐
Decovar
decovar.dev › blog › 2021 › 03 › 08 › cmake-cpp-library
Creating a C++ library with CMake | Declaration of VAR
March 8, 2021 - In this case the library is declared as STATIC, but actually it is not a good idea to hardcode libraries type like that in their project files, because CMake has a global flag for this exact purpose - BUILD_SHARED_LIBS - and in general it’s better to rely on that flag instead of setting libraries type inline.
🌐
CMake
cmake.org › cmake › help › v3.23 › guide › tutorial › Selecting Static or Shared Libraries.html
Step 9: Selecting Static or Shared Libraries — CMake 3.23.5 Documentation
This will also mean that USE_MYMATH will not control building MathFunctions, but instead will control the behavior of this library. The first step is to update the starting section of the top-level CMakeLists.txt to look like: ... cmake_minimum_required(VERSION 3.10) # set the project name and version project(Tutorial VERSION 1.0) # specify the C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) # control where the static and shared libraries are built so that on windows # we don't need to tinker with the path to run the executable set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "
🌐
CMake
cmake.cmake.narkive.com › TNONT3iP › use-a-static-library
[Cmake] use a static library
That's what I've done (making a libvtk_qt.a ) but now I still have linking problems. I've joined my cmakelists and error listing. Thanks for helping · Post by Andy Cedilnik Hi, ADD_LIBRARY(foo STATIC source.c source1.c...) TARGET_LINK_LIBRARIES(bar foo) or TARGET_LINK_LIBRARIES(bar /path/to/foo/libfoo.a) Andy
🌐
Medium
medium.com › @jacob.m.adams › creating-shared-static-libraries-with-cmake-7e5ae160c59d
Creating Shared/Static Libraries with CMake | by Jacob M Adams | Medium
December 23, 2022 - The “libtrout.a” is your static library. If you configured cMake to make a shared library then you would see “libtrout.so”.
🌐
SciVision
scivision.dev › cmake-force-link-static-libs
CMake force linking static libs | Scientific Computing
September 28, 2025 - CMake by default searches for shared libs before static libs. Sometimes a project needs to specifically link external or internal static libs, even if shared libs are present. A simple cross-platform way to do this is to set CMAKE_FIND_LIBRARY_SUFFIXES before any find_library() or find_package():
🌐
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.
🌐
Cocos Forums
forum.cocosengine.org › cocos2d-x › c++
Building and linking external static lib to game with CMake - C++ - Cocos Forums
May 22, 2018 - I have a library that I need to add to the game, and while I have it working using a VS project file for Win32 and Android.mk (LOCAL_STATIC_LIBRARIES += mylib_static) for Android, I’m not quite sure how to add it the the…
🌐
GitHub
github.com › m3y54m › library-with-cmake
GitHub - m3y54m/library-with-cmake: Creating a shared or static library using cmake · GitHub
All the instructions needed to build the libraries and executables are described in CMakeLists.txt file. In order to generate the Makefile and other files used to build this project in a directory called build first go to the root of this repository and run this command: ... Having file hello.o we can create a static library for it using the following command.
Author   m3y54m
🌐
Cristianadam
cristianadam.eu › 20190501 › bundling-together-static-libraries-with-cmake
Bundling together static libraries with CMake - Cristian Adam
May 1, 2019 - The most common decision is to build as a shared library (BUILD_SHARED_LIBS set to TRUE in the CMake script). The open source dependencies could be also shared libraries, or static libraries. If they are shared libraries you need to take care of deployment.