Skip to content

Instantly share code, notes, and snippets.

@SofijaErkin
Last active March 21, 2022 03:50
Show Gist options
  • Save SofijaErkin/088b140e234706f5feb2aa07d8f64550 to your computer and use it in GitHub Desktop.
Save SofijaErkin/088b140e234706f5feb2aa07d8f64550 to your computer and use it in GitHub Desktop.
VSCode config CMake for buildng, compiling, debugging C11/C++11 on mac.

CMake Manually VSCode on mac(Simple C/C++ Source file Version)

This is a markdown for one C++ file to build, compile, debug using CMake in VSCode on mac.

This is my workspace environment!

Clang: 9.0.0;

Llvm-gcc: 4.2.1;

Visual Studio Code v1.63.2;

Code Runner v0.11.6 (Author: Jun Han);

CodeLLDB v1.4.5 or v1.5.0 (Author: Vadim Chugunov).

Now:

This workspace named hello-vsc, including 4 directories and 7 files.

The Architecture of workspace hello-vsc are:

hello-vsc

|

|_.vscode

|     |__c_cpp_properties.json

|     |__launch.json

|     |__settings.json

|     |__tasks.json

|__CMakeLists.txt

|_build

|     |_debug

|     |_release

|__cmake_compile_problem.md

|__hello_vsc.cpp

hello_vsc.cpp: This workspace main function.

cmake_compile_problem.md: This project witness problem while using CMake.

build: VSCode Build folder.

debug: VSCode Debug Version folder.

release: VSCode Release Version folder.

CMakeLists.txt: The project CMake file.

.vscode: VSCode configuration folder.

tasks.json: This task will invoke the Clang C++ compiler to create an

executable file from the source code.

settings.json: Workspace Settings.

launch.json: configure VS Code to launch the LLDB debugger when you press

F5 to debug the program.

c_cpp_properties.json: allows you to change settings such as the path to

the compiler, include paths.

Notices:

You only need to modify the Include path setting if your program includes

header files that are not in your workspace or the standard library path.

Especially: "includePath": [ "/Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include"].

If your seprated Command-line tools's Clang version is 9.0.0, copy this program

directly.

If not, just use this command to checkout the Clang version of your seprated

Command-line tools:

cd /Library/Developer/CommandLineTools/usr/lib/clang/ && ls

That may show like this: 9.0.0 or 9.1.0 or 10.0.0 or X.X.X. As this may show,

The Clang version of your seprated Command-line tools is 9.0.0, or 9.0.1 or

10.0.0 or X.X.X. And just replace my 9.0.0.

If you do not have a seprated Command-line tools, just install a seprated command

line tools for your Mac depencing your macOS System.

I will add the 7 file via adding file.

END.

Reference:

1.Build and Debug C++ on Visual Studio Code for Mac.

2.Visual Studio Code Build and Debug a C++ with CMake on Mac OS.

3.C++ Development using Visual Studio Code, CMake and LLDB.

4.[C++] 令和最新版 C++をVisual Studio Codeで書きたい.

5.[C++/Mac] C++をVisual Studio Codeで書きたい.

6.Visual Studio Code を使った C++ のビルド&デバッグ方法(with CMAKE).

7.VSCode でC++の開発環境構築(Windows / MacOS).

8.Visual Studio の CMake プロジェクト.

9.Build C++ Project using CMake at VSCode on mac(Chinese Blog)

(That has solved My Deubg trouble. Thanks!)

{
"configurations": [
{
"name": "Mac",
// to use other third party libraries, the “includePath” array is
// the place to add more paths so VSC can provide auto completion
// information.
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}

CMake Compile Problem

This is a markdown for the problem of this hello-vsc project to compile via CMake.

First, roadmap of using CMake in VSCode on mac:

cmake:

type `⇧+⌘+P` and type “`Tasks`” and pick “`Run Task`”

From the list pick “`cmake`” and pick "`contiue xxx`"

make:

type `⇧+⌘+P` and type “`Tasks`” and but this time pick “`Run Build Task`”

From the list pick “`make`” and pick "`contiue xxx`"

debug:

On VSC click on the little bug icon on the Sidebar

Click the Configure Icon on the Debug View (the little gear with a red badge on it)

From the list select LLDB

(if you also install the extension CodeLLDB, select the second one)

(if you do not install the extension CodeLLDB, select the first one)

if use Native Debug extension, then you can add lldb-mi to your path using ln -s

/Applications/Xcode.app/Contents/Developer/usr/bin/lldb-mi /usr/local/bin/lldb-mi

if you have Xcode. But This does not work! Please do not worry to cancel the

soft-link.

This equals ~/.vscode/settings.json "code-runner.executorMap": ["cpp": "xxx"]

during using coder-runner extensions. I guess!!!

1.Problem A

1.1Operate RoadMap

echo: echo Hello

cmake: ⇧+⌘+P and type “Tasks” and pick “Run Task” From the list pick “cmake” and pick "contiue xxx"

Run Code: ^\~N

1.2Output

echo:

Executing task: echo Hello <

Hello

Terminal will be reused by tasks, press any key to close it.

cmake:

> Executing task: cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug .. <

-- The C compiler identification is AppleClang 8.1.0.8020042
-- The CXX compiler identification is AppleClang 8.1.0.8020042
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
CMake Error at CMakeLists.txt:8 (add_executable):
  Cannot find source file:

    hello-vsc.cpp

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
  .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc


CMake Error at CMakeLists.txt:8 (add_executable):
No SOURCES given to target: hello-vsc


CMake Generate step failed.  Build files cannot be regenerated correctly.
The terminal process "/bin/zsh '-c', 'cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ..'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

make:

> Executing task: make -j 8 <

[ 50%] Building CXX object CMakeFiles/hello-vsc.dir/hello_vsc.cpp.o

[100%] Linking CXX executable hello-vsc

[100%] Built target hello-vsc

Terminal will be reused by tasks, press any key to close it.

Run Code:

[Running] cd "/Users/marryme/VSCode/CppProject/hello-vsc/" && g++ hello_vsc.cpp -o hello_vsc && "/Users/yq/VSCode/CppProject/hello-vsc/"hello_vsc

hello Visual Studio Code! :)

[Done] exited with code=0 in 0.449 seconds

1.3Run Or Not

cmake: Not(Before);

make: Run.

Run Code: Run. Genrate hello_vsc target file in ~/./.

1.4Fix Or Not

cmake: Fixed. Adjust set(SOURCE hello-vsc.cpp) to set(SOURCE hello_vsc.cpp). Also, Output:

Executing task: cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug .. <

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/yq/VSCode/CppProject/hello-vsc/build

Terminal will be reused by tasks, press any key to close it.

2.Problem B

2.1Operate RoadMap

After add lldb-mi dibbuger to your PATH on your .bash_profile the location of

the lldb-mi debugger just re-launch VSC and you should be able to debug your

code by just clicking the “Start Debugging (F5)” button.

2.2Output

Debug button fresh! Debug did not work.

2.3Run Or Not

Not

2.4May be

lanuch.json under ~/.vscode/ needed to be changed.

2.5Fix Or Not

Fix But Not successfully fixed. Add ""program": "${workspaceFolder}/${fileBasenameNoExtension}", // Have changed", but DEBUG CONSOLE Output:

Launching: /Users/yq/VSCode/CppProject/hello-vsc/hello_vsc

Process exited with code 0.

Debug do not work.

Debug as Output target file.

hello Visual Studio Code! :)

marryme@rose-2  ~/VSCode/CppProject/hello-vsc  

May be need to fix later. Even add "//" before ""target": "./bin/hello-vsc"," in lanuch.json, not fix, Debug as Output target file.

2.6Other Fix Or Not

Yes. add ","preLaunchTask": "Build"" to ~/.vscode/lanuch.json, add The below thins to ~/.vscode/tasks.json:

,
{
    "type": "shell",
    "label": "Build",
    "dependsOrder": "sequence",
    "dependsOn":[
        "cmake",
        "make"
    ]
}

But "DEBUG CONSOLE" output:

Launching: /Users/yq/VSCode/CppProject/hello-vsc/hello_vsc

Process exited with code 0.

"TERMINAL" output:

hello Visual Studio Code! :)
marryme@rose-2  ~/VSCode/CppProject/hello-vsc  

Did Not Fixed! Debug as output the target file.

Added Fix Output:

Internal debugger error: unable to find executable for '/Users/yq/VSCode/CppProject/hello-vsc/build/hello_vsc'

Did Not Fix.

BREAK FIXED!

WHAT HAPPENS?

Your know, I set the project name "hello-vsc" in CMakeLists.txt before 

BREAK FIXED, and set the debug name "hello_vsc" in ~/.vscode/lanuch.json.

when i cmake and make in the dirctory build, that will generate target file

hello-vsc. But when i click the debug button, that lanuch will to find 

target file hello_vsc. CAN YOU SEE? To be find target file "hello_vsc" 

differ from the generated target file "hello-vsc". That's it.

Notice:

CMake Warning:

  No source or binary directory provided.  Both will be assumed to be the

  same as the current working directory, but note that this warning will

  become a fatal error in future CMake releases.

3.END

The C++ Project build, compile, debug using CMake at VSCode on mac, that has finished.

4.Reference:

1.Build and Debug C++ on Visual Studio Code for Mac.

2.Visual Studio Code Build and Debug a C++ with CMake on Mac OS.

3.C++ Development using Visual Studio Code, CMake and LLDB.

4.[C++] 令和最新版 C++をVisual Studio Codeで書きたい.

5.[C++/Mac] C++をVisual Studio Codeで書きたい.

6.Visual Studio Code を使った C++ のビルド&デバッグ方法(with CMAKE).

7.VSCode でC++の開発環境構築(Windows / MacOS).

8.Visual Studio の CMake プロジェクト.

9.Build C++ Project using CMake at VSCode on mac(Chinese Blog)

(That has solved My Deubg trouble. Thanks!)

# Reference: https://medium.com/@ivan.mejia/c-development-using-visual-studio-code-cmake-and-lldb-d0f13d38c563
cmake_minimum_required(VERSION 3.0)
# make sure to use as minimum version of cmake the 3.0
project(hello_vsc)
# set the project’s name to “hello-vac”
set(CMAKE_CXX_STANDARD 11)
# set(CMAKE_CXX_STANDARD_REQUIRED True)
set(SOURCE hello_vsc.cpp)
# the source code of our project is in “hello-vsc.cpp”
include(CTest)
enable_testing()
add_executable(${PROJECT_NAME} ${SOURCE})
# add more files separated by one or more spaces and finally we set our
# executable’s using the same name we use as the project name and declaring the
# sources code to be included during the build process.
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
// Reference: https://medium.com/@ivan.mejia/c-development-using-visual-studio-code-cmake-and-lldb-d0f13d38c563
#include <iostream>
int main(int argc, const char * argv[]) {
std::cout << "hello Visual Studio Code! :)" << '\n';
return 0;
}
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "lldb",
"request": "launch",
// Added to fix: Changed "program":
"program": "${workspaceFolder}/build/debug/${fileBasenameNoExtension}", // Have changed
// What that "program" means?
// "target": "./bin/hello-vsc", // that’ll make the debugger run and
// attach to our executable inside VSC.
"cwd": "${workspaceRoot}", // What that means?
"valuesFormatting": "parseText",
"envirment": [], // Added to fix.
"externalConsole": false, // Added to fix.
"preLaunchTask": "Build"
}
]
}
{
"files.defaultLanguage": "c++",
"editor.suggest.snippetsPreventQuickSuggestions": false,
"editor.acceptSuggestionOnEnter": "off",
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
"c": "cd $dir && cd build/debug && cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ../.. && make -j 8 && $dirbuild/debug/$fileNameWithoutExt",
"cpp": "cd $dir && cd build/debug && cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ../.. && make -j 8 && $dirbuild/debug/$fileNameWithoutExt"
},
"code-runner.saveFileBeforeRun": true,
"code-runner.preserveFocus": false,
"code-runner.clearPreviousOutput": false,
"code-runner.ignoreSelection": true,
// "C_Cpp.clang_format_sortIncludes": true,
"editor.formatOnType": true,
"clang.cxxflags": [
"-std=c++11"
],
"clang.cflags": [
"-std=c11"
],
"clang.completion.enable": true
}
{
// Reference: https://medium.com/@ivan.mejia/c-development-using-visual-studio-code-cmake-and-lldb-d0f13d38c563
// Reference:
// Build C++ Project using CMake at VSCode on mac(Chinese Blog)
// https://blog.csdn.net/Gou_Hailong/article/details/119939307
// That has solved My Deubg trouble. Thanks!
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {
"cwd": "${workspaceRoot}/build/debug" // What that "cwd" means?
},
// the “options” section where is used the property “cwd” (current working
// directory) and the substitution variable “${workspaceRoot}”, this
// variable contains the path to the folder where we load our project in
// VSC. We concat the “build” directory which should be created under our
// project’s root directory.
"tasks": [
{
// The task “cmake” which allows us to generate all the build files
// required to generate a binary executable.
"type": "shell",
"label": "cmake",
// "command": "echo Hello"
"command": "cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ../.."
// Basically instructs CMake to generate the build files and set it
// up to instruct the C++ compiler to avoid using optimizations and
// generate debug code;
// finally the “..” indicate to use the ../CMakeLists.txt file as
// the input of CMake.
},
{
// The task “make” that allows us to build our executable.
"type": "shell",
"label": "make",
// "command": "echo Hello"
"command": "make -j 8",
// basically instructs make to start building our program and use 8
// jobs that can run simultaneously in each core of your computer’s
// CPU,
// even when in this case this argument is not necessary you can
// leave it in case you have more than 8 source files,
// that’ll make the compiler to compile all the sources in parallel
/// assigning each source to one core and leaving the rest to other
// cores as soon as they get free of work.
// "isBuildCommand": true
// the property “isBuildCommand” set to true enables the task to be
// executed via the Tasks: Run Build Task.
// "The property isBuildCommand is deprecated. Use the group
// property instead. See also the 1.14 release notes."
"presentation": {
"echo": true,
"reveal": "always",
"panel": "shared"
}
},
{
"type": "shell",
"label": "Build",
"dependsOrder": "sequence",
"dependsOn": [
"cmake ",
"make"
]
}
]
// The property “tasks” is an array of tasks we require.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment