Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - Code::Blocks - hello world - basic example

Where can I find a guide to generate a simple CMake Hello World project to be loaded in CMake?

platform: Lenovo 32bit Linux Kubuntu

1) I would be using a git repo:

./git/CMakeLists.txt
./git/code/CMakeLists.txt
./git/code/hello-world.c

where the files contain the obvious content

2) I would run cmake

- pointing the source to the git repo indicated in 1
- configuring the repo
- generating the code-blocs-project (cbp) file in ./build

3) so I can simply click

- the cbp link in ./build
- compile the project in c::b and run a 
- very basic console program spitting out, you guessed it: "Hello stack overflowers!"
like image 940
hewi Avatar asked Apr 01 '15 19:04

hewi


1 Answers

So, just to confirm the files' obvious content; here's what I have:

~/devel/example $ tree .
.
├── build
└── git
    ├── CMakeLists.txt
    └── code
        ├── CMakeLists.txt
        └── hello-world.c

3 directories, 3 files

~/devel/example $ cat git/CMakeLists.txt 
cmake_minimum_required(VERSION 3.0)
project(Hello)
add_subdirectory(code)

~/devel/example $ cat git/code/CMakeLists.txt 
add_executable(hello hello-world.c)

~/devel/example $ cat git/code/hello-world.c 
#include <stdio.h>

int main() {
  printf("Hello stack overflowers!\n");
  return 0;
}

Now, to run CMake I did:

~/devel/example $ cd build/
~/devel/example/build $ cmake ../git -G"CodeBlocks - Unix Makefiles"
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/fraser/devel/example/build
~/devel/example/build $ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  code  Hello.cbp  Makefile

which you can see has resulted in a CodeBlocks project file (Hello.cbp)

If you now open this project in CodeBlocks (double click on the project file), you should see the project Hello on the left pane.

By default, the "all" target is selected. It should appear in a drop-down box in the compiler toolbar at the top of the GUI. This builds all the targets specified in the project, but isn't something you can run - you can only build it.

The executable target's name is "hello", as specified in the CMake code add_executable(hello hello-world.c). To run the executable, select "hello" from the drop-down box mentioned before, and then hit the "Build and run" icon in the same toolbar.

like image 120
Fraser Avatar answered Oct 04 '22 22:10

Fraser