1. 单个或者较少源文件的编译
gcc -o test test.c
gcc -o test test.c test1.c
参考文档:
a. https://man7.org/linux/man-pages/man1/gcc.1.html
2. 使用Makefile进行编译
这里使用CMake工具自动生成Makefile文件
安装CMake:sudo apt-get install cmake
创建目录:mkdir makefile
进入目录:cd makefile
创建目录:mkdir bin build src include
bin用于存放可执行程序,build 存放编译过程中产生的中间文件,src存放源文件,include 存放头文件
创建文件:touch CMakeLists.txt
编辑文件:nano CMakeLists.txt
project(main)
add_subdirectory(src)
进入src目录:添加源文件 main.c age.c CMakeLists.txt
编辑src目录下的CMakeLists.txt
aux_source_directory(. SRC_LIST) include_directories(../include) add_executable(main ${SRC_LIST}) set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
进入include目录:添加头文件 age.h
进入build目录:cmake ..
接着:make
进入bin目录:./main
架构图:
本文来自博客园,作者:Clay,转载请注明原文链接:https://www.cnblogs.com/clayyjh/p/14526902.html
浙公网安备 33010602011771号