How compile only c with cmake?

c, cmake

Solution

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.

See the documentation for the `project` command. The syntax is slightly different when specifying additional parameters such as a version or description.

Problem

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 ```

Original source