CMake - Code::Blocks - hello world - 基础示例

5

我希望你能为我提供一个简单的CMake Hello World项目生成指南,以便在CMake中加载。

平台:联想32位Linux Kubuntu

1)我将使用git仓库:

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

当文件包含明显的内容时

2) 我会运行cmake

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

3) 所以我只需点击即可

- 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!"

第二点是正确的,第三点只是编写程序的问题。问题在于第一点:因此,您需要知道如何编写一个cmake项目,以便从源文件创建一些可执行文件和库,从而可以生成您的codeblock项目,然后在codeblock中构建和运行您的项目。所以,现在您知道要搜索什么了:一个好的cmake教程。 - Antonio
1
问题是没有一个CMake教程是简单的并包含文件夹结构的想法,非常感谢Fraser提供的好例子! - hewi
你应该接受Fraser的优秀答案Howto - Antonio
1
Antonio:感谢您指出这一点。 - hewi
1个回答

13

所以,只是确认文件显而易见的内容;以下就是我拥有的东西:

~/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;
}
现在,要运行CMake,我做了以下操作:

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

现在你可以看到已经生成了一个CodeBlocks项目文件(Hello.cbp)。

如果现在你在CodeBlocks中打开这个项目(双击项目文件),你应该会在左侧面板看到Hello项目。

默认情况下,"all"目标被选中。它应该出现在GUI顶部的编译器工具栏的下拉框中。它将构建项目中指定的所有目标,但不能运行 - 只能构建它们。

可执行目标的名称是“hello”,如CMake代码add_executable(hello hello-world.c)中所指定的那样。要运行可执行文件,请从之前提到的下拉框中选择“hello”,然后点击同一工具栏中的“Build and run”图标。


1
我记得很早以前我曾经为基础知识苦苦挣扎,这并不好玩!你只想快点进入编写代码的部分,而不是在设置方面瞎搞。 - Fraser

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接