ds.*_*ane 7 c++ macos visual-studio-code
我正在尝试在 macOS 上使用 clang++ 构建一个包含多个 *.cpp 文件的简单项目。
任务.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-Wall",
"-Wextra",
"-Weffc++",
"-Wconversion",
"-pedantic-errors",
"-stdlib=libc++",
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
结果命令如下所示:
/usr/bin/clang++ -std=c++17 -Wall -Wextra -Weffc++ -Wconversion -pedantic-errors -stdlib=libc++ -g '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp' -o '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/main'
Run Code Online (Sandbox Code Playgroud)
但编译器会抛出错误:
clang: error: no such file or directory: '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp'
Run Code Online (Sandbox Code Playgroud)
但是如果我更改指向文件的字符串:
'/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp'
Run Code Online (Sandbox Code Playgroud)
对此(不带引号并添加“\”):
/Users/USER/Developer/WORKINGDIR/Lesson\ 2/Lesson\ 2.8/*.cpp
Run Code Online (Sandbox Code Playgroud)
一切编译都很好。
但是如何在tasks.json 中配置相同的字符串呢?或者我应该在那里更改什么才能使其正常工作?
兄弟!
最重要的建议是,建议您不要在目录或文件名中使用空格。
您的多源文件架构是这样的:
fitBodyBootCamp
|
|_.vscode
| |__c_cpp_properties.json
| |__launch.json
| |__settings.json
| |__tasks.json
|__test.cpp
|__test.h
|__testImp.cpp
Run Code Online (Sandbox Code Playgroud)
如果是这样,则这是一个在同一文件夹下具有多个源文件的项目。
你的“ tasks.json”应该是:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Compile With clang++",
//"command": "clang++",/usr/bin/clang++
"command": "/usr/bin/clang++",
"args": [
"-std=c++11",
"-stdlib=libc++",
// My project fitBodyBootCamp were under
// /Users/marryme/VSCode/CppProject/fitBodyBootCamp
// So ${workspcaeFolder} also were
// /Users/marryme/VSCode/CppProject/fitBodyBootCamp
// all the *.cpp files were under
// /Users/marryme/VSCode/CppProject/fitBodyBootCamp
"${workspaceFolder}/*.cpp", // Have changed
"-o",
// Thanks those chiense website bloggers!
// 1.mac vscode compile c++ multi-directory code demo
// https://blog.csdn.net/fangfengzhen115/article/details/121496770?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4.pc_relevant_default&spm=1001.2101.3001.4242.3&utm_relevant_index=7
// 2.Compile and debug c++ multi-folder project under VSCode (non-makefile)
// https://blog.csdn.net/BaiRuichang/article/details/106463035
// I also put the main.o under my current workspace directory
// This after "-o" is the target file test.o or test.out or test
"${workspaceFolder}/${fileBasenameNoExtension}", // Have changed
"-I",
// This after "-I" if the include files directory
"${workspaceFolder}", // Have changed
"-Wall",
"-g"
],
"options": {
// "cwd" is the source files directory
"cwd": "${workspaceFolder}" // Have changed
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果您使用gcc/g++,只需将 更改clang/clang++为这些即可。
如果此行代码需要更改或运行,我添加注释
” // Have changed“。只是注意到那行代码!请!
》tasks.json:你知道吗,那就是《tasks.json》!!!不是 ” task.json”。
更多关于“ launch.json”或“ c_cpp_properties.json”或
” settings.json“,
请参考GitHub Gist 中的手动使用多个 CXX 相同文件夹VSCode(mac),
或者,参考Github 上的手动使用多个 CXX 相同文件夹VSCode(mac)。
或者你的多源文件架构如下:
fitBody
|
|_.vscode
| |__c_cpp_properties.json
| |__launch.json
| |__settings.json
| |__tasks.json
|__build
|__inc
| |__fitbody.h
|__src
|__fitbody.cpp
|__main.cpp
Run Code Online (Sandbox Code Playgroud)
如果是这样,那就是分离了多个源文件。
你的“ tasks.json”就像这样:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Compile With clang++",
// Just use clang compiler
//"command": "clang++",/usr/bin/clang++
"command": "/usr/bin/clang++", // Have changed
"args": [
"-std=c++11",
"-stdlib=libc++",
// My project fitBodyBootCamp were under
// /Users/marryme/VSCode/CppProject/fitBody
// So ${workspcaeFolder} also were
// /Users/marryme/VSCode/CppProject/fitBody
// all the *.cpp files were under
// /Users/marryme/VSCode/CppProject/fitBody/src
// this is the source files diretory
"${workspaceFolder}/src/*.cpp", // Have changed
"-o",
// Thanks those chiense website bloggers!
// 1.mac vscode compile c++ multi-directory code demo
// https://blog.csdn.net/fangfengzhen115/article/details/121496770?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4.pc_relevant_default&spm=1001.2101.3001.4242.3&utm_relevant_index=7
// 2.Compile and debug c++ multi-folder project under VSCode (non-makefile)
// https://blog.csdn.net/BaiRuichang/article/details/106463035
// I also put the main.o under my build folder directory ../build
// This after "-o" is the target file main.o or main.out or main
"${workspaceFolder}/build/${fileBasenameNoExtension}", // Have changed
"-I",
// This is include directory.
"${workspaceFolder}/inc", // Have changed
"-Wall",
"-g"
],
"options": {
// "cwd" is the source files directory
"cwd": "${workspaceFolder}/src" // Have changed
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
更多关于“ launch.json”或“ c_cpp_properties.json”或
” settings.json“,
请参考GitHub Gist 中的手动分离多个 CXX VScode(mac)。
或者,参考GitHub 上的“手动分离多个 CXX VScode(mac)”。
结尾!
不要复杂化tasks.json。最好使用诸如 CMake 之类的东西来处理所有这些配置(C++ 版本、编译标志和警告等)并仅调用make(或ninja或您使用 CMake 选择的任何其他生成器)。
然后,在tasks.json“选项”中的“cwd”值将指向构建文件夹(运行 CMake 命令的文件夹)而不是源文件夹,“args”将是 CMake 中目标的名称(或空)如果你想构建所有目标),“命令”将是“/usr/bin/ninja”或“/usr/bin/make”之类的东西,具体取决于你在 CMake 中选择的生成器。
这还有一个优点,即其他人可以从中受益(或者为您创建这个),无论他们的编辑是谁。
您正在使用的标志的可能CMakeLists.txt文件类似于
project(TheProjectName)
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
add_compile_options(-Wall)
add_compile_options(-Wextra)
add_compile_options(-pedantic-errors)
add_compile_options(-Weffc++)
add_compile_options(-Wconversion)
add_compile_options(-stdlib=libc++)
# This is the executable target "main" is the name of the executable
# and you can change to something else
add_executable(main main.cpp
other_file.h
other_file.cpp
some_other_file.h
some_other_file.cpp
)
Run Code Online (Sandbox Code Playgroud)
-g中未设置指定调试版本的选项CMakeLists.txt。相反,当您运行时,cmake您将其构建类型设置为Debug.
要使用此CMakeLists.txt文件,请build在工作区文件夹中创建一个文件夹并在终端中转到它。然后运行该命令
cmake .. -DCMAKE_BUILD_TYPE=Debug一次。make之后,您可以仅从该文件夹编译项目build。设置此项tasks.json只是将“cwd”设置为指向${workspaceFolder}/build并将命令设置为/usr/bin/make。
最后一点,如果您同时安装了 clang 和 gcc,那么当您运行时cmake它可能会使用 gcc 而不是 clang。要专门针对 clang 运行cmake命令,指定 CXX 和 CC 环境变量以指向 clang。也就是说,cmake 命令应该运行(从构建文件夹一次):
CXX=clang++ CC=clang cmake .. -DCMAKE_BUILD_TYPE=Debug
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6605 次 |
| 最近记录: |