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
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With