C-Programmierung

Compilation mit CMake

Kompilation mit Unix | | Open-Source Software

CMake = cross plattform build tool

Example project with a main and one library module:

main.c:

#include <stdio.h> /* system headers */
#include "module.h" /* local headers */

int main() /* main function */
{
   double x=10; /* local initialized variable */

   /* function calls to other modules go here: */

   printf("square of %g is %g\n", x, square(x));

   return(0); /* error code 0 means ok */
}

module.h:

#ifndef MODULE_H /* guard */
#define MODULE_H

/* function prototypes of this module go here: */

double square(double x);

#endif /* end of guard */

module.c:

#include "module.h"

/* function implementations of this module go here: */

double square(double x)
{
   return(x*x);
}

CMakeLists.txt:

# cmake build file

PROJECT(MyProject)

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98")

# header and module list

SET(LIB_HDRS
   module.h
   )

SET(LIB_SRCS
   module.c
   )

# library

SET(LIB_NAME ${PROJECT_NAME})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

ADD_LIBRARY(${LIB_NAME} ${LIB_SRCS} ${LIB_HDRS})

# executable

ADD_EXECUTABLE(main main.c)
TARGET_LINK_LIBRARIES(main
   ${LIB_NAME}
   )

To compile you type:

cmake .
make

To execute you type:

./main

Or all at once:

cmake . && make && ./main

Access the example via WebSVN:

Checkout the CMake example via SVN:

svn co svn://schorsch.efi.fh-nuernberg.de/cmake.cpp


Kompilation mit Unix | | Open-Source Software

Options: