Skip to content

Instantly share code, notes, and snippets.

@SofijaErkin
Last active March 21, 2022 05:46
Show Gist options
  • Save SofijaErkin/3e0ce06d9bc001028a88da8ba7f812a9 to your computer and use it in GitHub Desktop.
Save SofijaErkin/3e0ce06d9bc001028a88da8ba7f812a9 to your computer and use it in GitHub Desktop.
MacOS Setup Seperated include, Multiple C/C++ Compile and Debug using CMake on VSCode

Seperated include Multi-C/C++ Manually CMake VSCode on Mac

This is a markdown for multiple 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 practiseCMake, including 5 directories and 9 files.

The Architecture of workspace practiseCMake are:

practiseCMake

|

|_.vscode

|     |__c_cpp_properties.json

|     |__launch.json

|     |__settings.json

|     |__tasks.json

|__CMakeLists.txt

|__Pet.cpp

|_build

|     |_debug

|     |_release

|_include

|     |__Pet.h

|__main.cpp

|__vscode_cmake_setup_problem.md

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

main.cpp: This workspace main function.

include: project include folder.

Pet.h: class Pet include function.

build: VSCode Build folder.

debug: VSCode Debug Version folder.

release: VSCode Release Version folder.

Pet.cpp: class Pet source function.

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.

I will add the 9 file via adding file.

{
"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}/include/**"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
# Reference: https://medium.com/@dexterchan_44737/visual-studio-code-build-and-debug-a-c-with-cmake-on-mac-os-7633bc59bf34
cmake_minimum_required(VERSION 3.0.0)
project(HelloWorld VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 11)
# set(CMAKE_CXX_STANDARD_REQUIRED True)
include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
file(GLOB SOURCES "*.cpp")
include(CTest)
enable_testing()
add_executable(main ${SOURCES})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
{
// 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": "(lldb)Launch Debug",
"type":"lldb",
"request": "launch",
// "program" is the target file diretory
// "program": "${fileDirname}/${fileBasenameNoExtension}",
"program": "${workspaceFolder}/build/debug/${fileBasenameNoExtension}", // Have changed
"args": [],
"stopAtEntry": false,
// Changes the current working directory directive ("cwd") to the folder
// where main.cpp is.
// This "cwd" is the same as "cwd" in the tasks.json
// That's the source files directory
"cwd": "${workspaceFolder}", // Have changed
"environment": [],
"externalConsole": true,
// "MIMode": "lldb" // ?
// "preLaunchTask": "Compile With clang++"
"preLaunchTask": "Build Debug Version"
}
]
}
// Reference: https://medium.com/@dexterchan_44737/visual-studio-code-build-and-debug-a-c-with-cmake-on-mac-os-7633bc59bf34
#include <iostream>
#include "include/Pet.h"
#include <memory>
int main(int, char **)
{
std::cout << "Hello, world!\n";
std::string n = "cat";
std::unique_ptr<Pet> pet(new Pet(n));
pet->eat();
}
// Reference: https://medium.com/@dexterchan_44737/visual-studio-code-build-and-debug-a-c-with-cmake-on-mac-os-7633bc59bf34
#include <iostream>
#include "include/Pet.h"
Pet::Pet(std::string &_name){
this->name = _name;
}
void Pet::sound(){
std::cout << this->name << " sounds" << std::endl;
}
void Pet::eat(){
std::cout << this->name << " eats" << std::endl;
}
std::string & Pet::getName(){
return this->name;
}
// Reference: https://medium.com/@dexterchan_44737/visual-studio-code-build-and-debug-a-c-with-cmake-on-mac-os-7633bc59bf34
#include <iostream>
#include <string>
class Pet{
private:std::string name;
public:Pet(std::string& name);
void sound();
void eat();
std::string& getName();
};
{
"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/@dexterchan_44737/visual-studio-code-build-and-debug-a-c-with-cmake-on-mac-os-7633bc59bf34
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
// "isShellCommand":true,
"options": {
// {workspaceRoot} is the directory of your workspace.
// "cwd" is the source files directory.
// "cwd": "${workspaceRoot}/build"
"cwd": "${workspaceFolder}/build/debug" // Have changed
},
// Property “label” allows VSC to reference the task name when running the
// task.
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ../..",
"presentation": {
"echo": true,
"reveal": "always",
"panel": "shared"
}
},
{
"type": "shell",
"label": "make",
"command": "make -j 8",
// “make -j 8 “ mean running the compile in parallel with max 8
// source files.
"presentation": {
"echo": true,
"reveal": "always",
"panel": "shared"
},
// "isBuildCommand":false
// the property “isBuildCommand” set to true enables the task to be
// executed via the Tasks: Run Build Task.
},
{
"type": "shell",
"label": "Build Debug Version",
"dependsOrder": "sequence",
"dependsOn": [
"cmake ",
"make"
]
}
]
}

Problem Occur Setting-Up CMake in VSCode on mac

0.Problem 0

0.1Platform

The architecture is the same as "1.Problem A".

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

0.2Output

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
-- Generating done
-- Build files have been written to: /Users/yq/VSCode/CppProject/practiseCMake/build

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

make:

> Executing task: make -j 8 <

[ 66%] Building CXX object CMakeFiles/HelloWorld.dir/main.cpp.o

[ 66%] Building CXX object CMakeFiles/HelloWorld.dir/Pet.cpp.o

[100%] Linking CXX executable HelloWorld

[100%] Built target HelloWorld

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

debug:

Internal debugger error: unable to find executable for '/Users/yq/VSCode/CppProject/practiseCMake/main'

0.3Run Or Not

cmake: Run.

make: Run.

debug: Not.

0.4Fix Or Not

debug: Not.

1.Problem A

1.1Platform

See project architecture.

Just like:

HelloWorld

|

|_.vscode

|     |__c_cpp_properties.json

|     |__launch.json

|     |__settings.json

|     |__tasks.json

|_build

|_include

|     |__Pet.h

|_CMakeLists.txt 

|_main.cpp

|_Pet.cpp

1.2VSCode Terminal Output

[Running] cd "/Users/yq/VSCode/CppProject/practiseCMake/" && g++ main.cpp -o main && "/Users/yq/VSCode/CppProject/practiseCMake/"main

Undefined symbols for architecture x86_64:

    "Pet::eat()", referenced from:
       
        _main in main-44f8ef.o

    "Pet::Pet(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&)", referenced from:
  
        _main in main-44f8ef.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

[Done] exited with code=1 in 0.543 seconds

1.3Run OR Not

Not

1.4 Fix Or Not

Fixed.

Reconfig the settings.json:

{

    "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
}

2.Problem B

2.1Platform

Same as Problem A.

2.2VSCode Terminal Output

zsh: no such file or directory: /Users/marryme/VSCode/CppProject/practiseCMake/build/debug/main

2.3Run OR Not

Not.

2.4 Fix Or Not

Fixed.

Change item target name at CMakeLists.txt from:

add_executable(HelloWorld ${SOURCES})

into

add_executable(main ${SOURCES})

3.Problem C

3.1Platform

Same as Problem A.

3.2VSCode Debug Pop-Up Output

Errors exist after running preLaunchTask 'Build Debug Version'.

3.3Run OR Not

Not.

3.4 Fix Or Not

Fixed.

Change item "program" at lanuch.json from:

"program": "${workspaceFolder}/${fileBasenameNoExtension}",

into

"program": "${workspaceFolder}/build/debug/${fileBasenameNoExtension}",

4.Problem D

4.1Platform

Same as Problem A.

4.2VSCode Output

Debug Pop-Up:

The preLaunchTask 'Build Debug Version' terminated with exit code 2.

Terminal Output:

> Executing task: make -j 8 <

make: *** No targets specified and no makefile found.  Stop.

The terminal process "/bin/zsh '-c', 'make -j 8'" failed to launch (exit code: 2).

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

3.3Run OR Not

Not.

3.4 Fix Or Not

Fixed.

Change item cmake command at tasks.json from

"command": "cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug .."

into

"command": "cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ../.."

5.END

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

finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment