如QT 工程那样在源代码下用命令生成工程项目那样,
qmake -project name.pro
下面简单写一个基于cmake的小程序:
创建如下文件:build为空目录
cmake + vs code可以在任何平台使用
vscode:
安装编译器,选择编译器(必须在系统的环境变量中)
若上面扫描工具包的列表中没有我们要的编译,但我们已安装,则需要单独配置一下
在ubuntun 18.04中使用cmake
写cpp code,
#include <stdio.h>
int main(int argc,char** argv)
{
printf("cmake test\n");
return 0;
}
配置CMakeLists.txt (名字是固定的)
cmake_minimum_required(VERSION 3.10.2)
project(CmakeTest VERSION 1.0.0)
add_executable(CmakeTest main.cpp)
~/Code/CmakeTest$ cd build/
:~/Code/CmakeTest/build$ ls
~/Code/CmakeTest/build$ cmake ../ (CMakeLists.txt在上级目录中)
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- 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/toby/Code/CmakeTest/build
这样build下面就生成了工程的Makefile文件
未安装好的环境会编译报错
cmake .
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:3 (project):
No CMAKE_CXX_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
See also "/home/toby/code/enet/CMakeFiles/CMakeOutput.log".
See also "/home/toby/code/enet/CMakeFiles/CMakeError.log".
解决方法
sudo apt-get update
sudo apt-get install -y build-essential
然后直接make就编译好了,注意在Makefile所在路径中执行此命令
~/Code/CmakeTest/build$ make
Scanning dependencies of target CmakeTest
[ 50%] Building CXX object CMakeFiles/CmakeTest.dir/main.cpp.o
[100%] Linking CXX executable CmakeTest
[100%] Built target CmakeTest
执行成功
~/Code/CmakeTest/build$ ./CmakeTest
cmake test
调试中修改代码,重新make再执行即可
若有增减代码文件,需要重新生成Makefile (这时为cmake …/)
再编译执行
~/Code/CmakeTest/build$ make
Scanning dependencies of target CmakeTest
[ 50%] Building CXX object CMakeFiles/CmakeTest.dir/main.cpp.o
[100%] Linking CXX executable CmakeTest
[100%] Built target CmakeTest
~/Code/CmakeTest/build$ ./CmakeTest
cmake test
zzzz
CMakeLists.txt的一些用法
cmake_minimum_required(VERSION 3.10.2)
project(CmakeTest VERSION 1.0.0)
set(MAIN_SOURCE main.cpp)
add_executable(cmaketesst ${MAIN_SOURCE})
MESSAGE("zzz")
在cmake …/ 中打印信息
MESSAGE(“zzz”)
判断
添加编译生成静态动态库并在工程中引用
新文件:
TestApp.cpp
#include "TestApp.h"
int TestApp::func_add(int a,int b)
{
return a+b;
}
TestApp.h
#include <stdio.h>
namespace TestApp
{
int func_add(int a,int b);
}
在主函数中调用
main.cpp
#include <stdio.h>
#include "TestApp.h"
int main(int argc,char** argv)
{
printf("cmake test\n");
printf("zzz\n");
printf("TestApp func_add %d",TestApp::func_add(100,1));
return 0;
}
修改CMakeLists.txt
cmake_minimum_required(VERSION 3.10.2)
project(CmakeTest VERSION 1.0.0)
set(MAIN_SOURCE main.cpp)
MESSAGE("zzz")
add_executable(cmaketesst ${MAIN_SOURCE})
# add_library(func_add STATIC TestApp.cpp TestApp.h)
add_library(func_add SHARED TestApp.cpp TestApp.h)
target_link_libraries(cmaketesst func_add)
编译后执行
~/Code/CmakeTest/build$ ./cmaketesst
cmake test
zzz
TestApp func_add 101
cmake嵌套
~/Code/CmakeTest$ tree
.
├── CMakeCache.txt
├── CMakeLists.txt
├── MainApp
│ ├── CMakeFiles
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── cmaketesst.dir
│ │ │ ├── build.make
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── CXX.includecache
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.internal
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ ├── progress.make
│ │ │ └── src
│ │ └── progress.marks
│ ├── cmake_install.cmake
│ ├── CMakeLists.txt
│ ├── Makefile
│ └── src
│ └── main.cpp
├── Makefile
└── TestAddLib
├── CMakeFiles
│ ├── CMakeDirectoryInformation.cmake
│ ├── func_add.dir
│ │ ├── build.make
│ │ ├── cmake_clean.cmake
│ │ ├── CXX.includecache
│ │ ├── DependInfo.cmake
│ │ ├── depend.internal
│ │ ├── depend.make
│ │ ├── flags.make
│ │ ├── link.txt
│ │ ├── progress.make
│ │ └── src
│ └── progress.marks
├── cmake_install.cmake
├── CMakeLists.txt
├── Makefile
└── src
├── TestApp.cpp
└── TestApp.h
10 directories, 34 files
根目录下的CMakeLists.txt
~/Code/CmakeTest$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.10.2)
project(CmakeTest VERSION 1.0.0)
add_subdirectory(MainApp)
add_subdirectory(TestAddLib)
主程序下的CMakeLists.txt
cat MainApp/CMakeLists.txt
cmake_minimum_required(VERSION 3.10.2)
project(CmakeTest VERSION 1.0.0)
set(MAIN_SOURCE src/main.cpp)
MESSAGE("zzz")
add_executable(cmaketesst ${MAIN_SOURCE})
# add_library(func_add STATIC TestApp.cpp TestApp.h)
target_link_libraries(cmaketesst func_add)
所调用库的CMakeLists.txt
~/Code/CmakeTest$ cat TestAddLib/CMakeLists.txt
cmake_minimum_required(VERSION 3.10.2)
project(CmakeTest VERSION 1.0.0)
MESSAGE("zzz")
# add_library(func_add STATIC TestApp.cpp TestApp.h)
add_library(func_add
SHARED
src/TestApp.cpp
src/TestApp.h)
target_include_directories(func_add PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src) #将头文件加入环境变量
在根目录cmake . 后生成三个Makefile
~/Code/CmakeTest$ find . -iname Makefile
./MainApp/Makefile
./TestAddLib/Makefile
./Makefile
make编译后
在./MainApp/cmaketesst 生成可执行文件,即主目录下即add_executable声明的地方所在路径
在windows上使用Cmake
下载安装cmake,确保cmd中可以使用cmake命令(若不行,则将其加入系统的环境变量里)
windows中的vs code安装上cmake /cmake tool扩展
用cmake-gui生成Makefile
用mingw32-make编译(这里Qt安装路径下有)下载安装
mingw-w64
mingw-w64
课程实践项目用cmake-gui在windows上编译运行一个已有的大项目工程
cpp/c
c语言开发无法自动补全,智能提醒的问题
解决用vscode进行c语言开发无法自动补全,智能提醒的问题
然后使用快捷键 ctrl + shift + p ,搜索 settings ,然后打开文件settings.json 打开后,加
"editor.quickSuggestions": true,
"other": true,
"comments": false,
"strings": false
}
cpp自动补全:
VS CODE C/C++无代码补全
这里添加了
"C_Cpp.autocomplete": "Default",
自动补全有效的方法
要修改配置文件,在File->Preferences->Settings下,配置:
{
"editor.minimap.enabled": true,
"C_Cpp.autocomplete": "Default",
"[cpp]": {
"editor.quickSuggestions": true
},
"[c]": {
"editor.quickSuggestions": true
}
}
前进后退工具按键:
安装插件:
Back and Forward buttons
这样在下面就有对应的图标显示了
嵌入式中开发用cmake
第一步:
export PATH=$PATH:/linux-devkit/sysroots/x86_64-arago-linux/usr/bin
第二步:
cd imu
mkdir build
cd build
cmake ..
make
make install
最后获得一个install目录