web-dev-qa-db-fra.com

CMake - Création d'une bibliothèque statique

J'ai deux fichiers dans mon projet appelé Test4:

Structure.hStructure.c

Je souhaite créer une bibliothèque statique pouvant être chargée par d'autres projets souhaitant utiliser ces fichiers. Voici mon fichier CMake actuellement:

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

Lorsque je construis en utilisant ce fichier CMake, aucune bibliothèque statique n'est générée. Rien ne se passe. Est-ce que je fais quelque chose de mal?

J'utilise l'IDE CLion.

12
Hatefiend

La ligne add_library devrait suffire. Voir cet exemple de code que je viens d'écrire pour tester en créer un puis l'utiliser (sous Ubuntu 16.04):

Structure.h:

int sum( int a, int b );

Structure.c:

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

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

Et puis voici le résultat de son exécution:

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
19
ndeubert

J'ai eu le même problème. Ce qui m'a manqué est l'emplacement où les fichiers de construction sont créés.

CLion crée des bibliothèques ou des exectables dans le répertoire cmake-build-*. SiBuild, Execution, Deployment > CMake > Configuration est Debug, le fichier lib (.a) est créé sous cmake-build-debug.

2
noobar

Après avoir utilisé la commande cmake. Ensuite, le Makefile aura l'option suivante: make default_target, révoquez-le et vous aurez un fichier. annuaire. Visitez https://cmake.org/cmake/help/v3.0/command/add_library.html vous aidera!

0
Maurice Aiken