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 OverflowStatic library dependencies
CMake: why is there no easy way to build a static library that contains linked static libraries?
How can I include dependencies in a static library using CMake
static and dynamic libraries, single compile
Videos
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
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.
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
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.
CMake favours passing the full path to link libraries, so assuming libbingitup.a is in ${CMAKE_SOURCE_DIR}, doing the following should succeed:
add_executable(main main.cpp)
target_link_libraries(main ${CMAKE_SOURCE_DIR}/libbingitup.a)
If you don't want to include the full path, you can do
add_executable(main main.cpp)
target_link_libraries(main bingitup)
bingitup is the same name you'd give a target if you create the static library in a CMake project:
add_library(bingitup STATIC bingitup.cpp)
CMake automatically adds the lib to the front and the .a at the end on Linux, and .lib at the end on Windows.
If the library is external, you might want to add the path to the library using
link_directories(/path/to/libraries/)