cmake语法入门记录

  刚刚开始学习ROS,打算入机器人的坑了,参考教材是《ROS及其人开发实践》胡春旭编著 机械工业出版社 华章科技出品。本来以为可以按照书上的步骤一步步来,但是,too young to simple啊,程序员的苦逼日子开始了,特地记录如下。

  在学习ROS的helloworld程序时,发现,ROS中居然使用cmake编译,头大,一不做二不休,看书练习。记录如下,环境kubuntu 18.04系统,最新升级的,目录/home/municationk/WORKM/cmake/t1

  两个文件main.c

  1 #include <stdio.h>
  2 
  3 int main(int argc, char **argv)
  4 {
  5         printf("Hello world!
");
  6 
  7         return 0;
  8 }

  CMakeList.txt文件:

  1 PROJECT (HELLO)
  2 SET(SRC_LIST main.c)
  3 MESSAGE(STATUS "This is BINARY dir" ${HELLO_BINARY_DIR})
  4 MESSAGE(STATUS "This is SOURCE dir" ${HELLO_SOURCE_DIR})
  5 ADD_EXECUTABLE(hello ${SRC_LIST})

  代码说明:行号是为了更容易说明代码,实际输入时是不需要的,也不能要

       简单的hello代码,这个复杂,只是为了说明复杂的问题,为了不被代码的复杂性所干扰,因此才这么做。

  有了代码:执行命令:千万不要漏掉了后面的".",表示在当前目录下执行cmake命令

cmake .

  执行后,在当前目录/home/municationk/WORKM/cmake/t1中生成:

-rw-rw-r-- 1 municationk municationk 12596 8月   4 15:55 CMakeCache.txt
drwxrwxr-x 5 municationk municationk  4096 8月   4 16:17 CMakeFiles
-rw-rw-r-- 1 municationk municationk  1510 8月   4 15:55 cmake_install.cmake
-rw-rw-r-- 1 municationk municationk   188 8月   4 16:18 CMakeLists.txt
-rw-rw-r-- 1 municationk municationk    96 8月   4 15:50 main.c
-rw-rw-r-- 1 municationk municationk  4709 8月   4 16:17 Makefile

  其他的文件都可以不用关心,生成了Makefile文件,有了它就可以make了,

  接着继续执行命令:

make
municationk@developk:~/WORKM/cmake/t1$ make
-- This is BINARY dir/home/municationk/WORKM/cmake/t1
-- This is SOURCE dir/home/municationk/WORKM/cmake/t1
-- Configuring done
-- Generating done
-- Build files have been written to: /home/municationk/WORKM/cmake/t1
[100%] Built target hello

  信息如上,没有错误到达100%就对了,此时目录/home/municationk/WORKM/cmake/t1中生成:

-rw-rw-r-- 1 municationk municationk 12596 8月   4 15:55 CMakeCache.txt
drwxrwxr-x 5 municationk municationk  4096 8月   4 16:29 CMakeFiles
-rw-rw-r-- 1 municationk municationk  1510 8月   4 15:55 cmake_install.cmake
-rw-rw-r-- 1 municationk municationk   188 8月   4 16:18 CMakeLists.txt
-rwxrwxr-x 1 municationk municationk  8296 8月   4 16:17 hello
-rw-rw-r-- 1 municationk municationk    96 8月   4 15:50 main.c
-rw-rw-r-- 1 municationk municationk  4709 8月   4 16:29 Makefile

  最后执行命令:结果就出来了。

.hello
municationk@developk:~/WORKM/cmake/t1$ ./hello 
Hello world!
原文地址:https://www.cnblogs.com/guochaoxxl/p/11298650.html