Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How compile only c with cmake? [closed]

Tags:

c

cmake

I have a c code that I insist on compile it with gcc not any c++ compiler! I want a CMakeLists.txt for it! Could you help me? Here is my simple project:

main.c
like image 642
Aryan Avatar asked Aug 07 '13 09:08

Aryan


People also ask

Can I use CMake with C?

In the C/C++ ecosystem, the best tool for project configuration is CMake. CMake allows you to specify the build of a project, in files named CMakeLists. txt, with a simple syntax (much simpler than writing Makefiles).

Do I need to run make after CMake?

Run make anywhere Once you have run cmake, you do not need to run it again unless a) you create another build directory or b) your current build directory is destroyed or horribly broken. In either of these cases, follow the steps above and run cmake in the top level build directory.


1 Answers

That's the basics of cmake:

cmake_minimum_required(VERSION 2.6.0)

# here we specify that the project is C language only, so the default
# C compiler on the system will be used
project(myprogram C)

add_executable(myprogram main.c)

That's really all you need for compiling a C file into an executable.

Specifying your project as a C language project should be enough for your needs.

If that isn't enough, you can force the compiler on the command line while invoking cmake.

mkdir build && cd build
export CC=gcc    
cmake ..
make

or

mkdir build && cd build
cmake .. -DCMAKE_C_COMPILER=gcc
make

Please also note that, as mentioned in the comments, even when mixing languages in a project, CMake is smart enough to properly call the C compiler when encountering a file with the .c extension.

like image 180
SirDarius Avatar answered Sep 21 '22 10:09

SirDarius