Chr Chr - 1 year ago 117
C Question

Run c-code with cmake

I am new to programming and I have several c-files I want to run from the terminal in mac. I have installed cmake from homebrew and it seems to be installed correctly (when I type "brew install cmake", I get the message "Warning: cmake-3.6.3 already installed").

My problem is, that I don't know what to type next to compile/run the file. I'm sorry if this seems really basic, but I don't understand the answers I found on Google. I have changed the directory to the folder containing my files "cd /Users/..." and I have a CMakeList.txt file from a friend and put in the same folder.
I have tried typing "cmake ." and it creates a lot of new folders, but I doesn't print anything. I have a "printf"-command in my main.c.

Can anyone tell me, what I should type to make the code print to the terminal?

Answer Source

CMake doesn't run your program.

CMake generates a Makefile. This Makefile can be interpreted by "make" by calling "make" in the same directory (you can also specify other names or paths, but not needed here). "make" will call the compiler, linker and maybe some other stuff to build your program. At the end and with no errors, you have a executable named as written in the "add_executable" instruction in your CMakeLists.txt. To run this program it should be sufficient to type "./program_name" in the build directory.

One hint: It is better to create a subfolder for building. "mkdir build && cd build && cmake ../ && make".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download