CMake - Creating a static library
Asked Answered
M

4

47

I have two files in my project called Test4:

Structure.h Structure.c

I want to create a static library that can be loaded by other projects who want to use those files. Here is my CMake file currently:

cmake_minimum_required(VERSION 3.6)
project(Test4)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES Structure.c Structure.h)
add_library(Test4 STATIC ${SOURCE_FILES})

When I build using that CMake file, no static library is generated. Nothing happens. Am I doing something wrong?

I am using the CLion IDE.

Moffit answered 4/5, 2017 at 2:32 Comment(0)
P
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.

Plectron answered 10/11, 2017 at 2:31 Comment(0)
S
71

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
Shawndashawnee answered 4/5, 2017 at 3:50 Comment(1)
Did this solve the problem? I would like to know more, since I have a similar problem. I don't see how this differs from OP's code.Stallworth
P
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.

Plectron answered 10/11, 2017 at 2:31 Comment(0)
R
2

It solved my problem. I was looking to set up a CLion project with a C++ 23 client and static library. I am thankful for your posting because after I read it, I got my stuff working in minutes. For some reason I find CMake counter-intuitive and despite numerous references, I still lack straightforward guidance about how to do simple things that were easy in "make" Anyway, thanks. Here is what I ended up with:

`cmake_minimum_required(VERSION 3.26)
project(vpa)
set(CMAKE_CXX_STANDARD 23)

#########################
# Regarding the Library #
#########################
add_library(vpad STATIC vpad.cpp
        vpad.cpp        vpad.h)

################################
# Regarding the Client Program #
################################
add_executable(vpa main.cpp
        vpa.cpp         vpa.h
        vpad.h
)
target_link_libraries(vpa vpad)`
Rejuvenate answered 18/9, 2023 at 11:45 Comment(0)
L
1

After you use command cmake . Then the Makefile will have the option like make default_target , revoke it and then you get a .a file in you r directory. Visit https://cmake.org/cmake/help/v3.0/command/add_library.html will help you!

Lorient answered 14/9, 2018 at 3:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.