Skip to content

Instantly share code, notes, and snippets.

@SofijaErkin
Last active March 21, 2022 03:11
Show Gist options
  • Save SofijaErkin/e244a5bed8fe483e35f72efc12ca3f13 to your computer and use it in GitHub Desktop.
Save SofijaErkin/e244a5bed8fe483e35f72efc12ca3f13 to your computer and use it in GitHub Desktop.
A multiple C/C++ files under same folder build, compile, debug with VSCode on mac

Multiple C/C++ Same Folder Manually VSCode(mac)

This is a setup for multiple C/C++ files under same folder to build,

compile and debug with VSCode on mac.

Amazing Notice:

Please double check the filename of the VSCode configuration file.

e.g:

tasks.json

launch.json

c_cpp_properties.json

settings.json

A week ago, I made a mistake: I wrote "tasks.json" as "task.json". As a result,

VSCode always reports the error "cannot find tasks.json file". I always thought:

I wrote "task.json", why there is no "tasks.json". It took a week to change the

configuration file, only to find out today that there is a problem with the file

naming. Oh! I am silly.

Notice:

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 fitBodyBootCamp, including 1 directories, 7 files.

The Architecture of workspace fitBodyBootCamp are:

fitBodyBootCamp

|

|_.vscode

|     |__c_cpp_properties.json

|     |__launch.json

|     |__settings.json

|     |__tasks.json

|__test.cpp

|__test.h

|__testImp.cpp

test.cpp: This project main function.

testImp.cpp: This project connected source function.

test.cpp: This project connected include function.

.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.

{
"configurations": [
{
"name": "Mac",
"includePath": [
// This is the include files directory
"${workspaceFolder}/**", // Have Change
"/Library/Developer/CommandLineTools/usr/include",
"/Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/include",
"/usr/local/include",
"/Library/Developer/CommandLineTools/usr/include/c++/v1",
"/usr/include"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
{
// 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 With LLDB",
"type": "lldb",
"request": "launch",
// "program" is the target file diretory
"program": "${workspaceFolder}/${fileBasenameNoExtension}", // Have changed
"args": [],
"stopAtEntry": true,
//"cwd": "${workspaceFolder}/../build",// Have changed
//"cwd": "${fileDirName}", ${workspaceFolder}/../build
// 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": false,
"preLaunchTask": "Compile With clang++"
}
]
}
# Notices--------------------------------------------------
# This Seprated Multiple CXX Manully VScode(mac) Do Not USE Makefile.
# This Seprated Multiple CXX Manully VScode(mac) Do Not USE Makefile.
# This Seprated Multiple CXX Manully VScode(mac) Do Not USE Makefile.
# ---------------------------------------------------------
# Thanks, Job Vranish.
# (https://spin.atomicobject.com/2016/08/26/makefile-c-projects/)
# Reference: Makefile Tutorial
# (https://makefiletutorial.com/)
# Reference: @yagiyuki from Qiita
# (https://qiita.com/yagiyuki/items/ff343d381d9477e89f3b)
# Reference: simonsso from Github
# (https://github.com/simonsso/empty-cpp-project/blob/master/Makefile)
# (1)Compiler
CXX = clang++
# (2)Compile options
CXX_FLAGS = -Wall -Wextra -std=c++11 -g
# (3)Build task directory path
BUILD_DIR := .
# (4)Source files directory path
SRC_DIRS := .
# (5)Library files directory path
LIBDIR :=
# (6)Add library files
LIBS :=
# (7)Target file, excutable file.
#TARGET := main fitbodyapp
# This main function test
TARGET :=test
# (8)Source files(code), to be compiled
# Find source files we want to compile
# *expression must around by single quotos
# SRCS = $(wildcard ./src/*.cpp)
SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s')
# (9)Object files
# String substituion for every C/C++ file
# e.g: ./src/bank.cpp urns into ./build/bank.cpp.o
# OBJS = $(patsubst %cpp,%.o,$(SRCS))
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
# (10)Dependency files
DEPS := $(OBJS:.o=.d)
# (11)Include files directory path
# Every folder in ./src find include files to be passed via clang
# INCDIR = -I./inc
# INC_DIR = ./inc
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
# (12)Include files add together a prefix, clang make sense that -I flag
# INCS = -I$(INC_DIR)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
# (13)Make Makefiles output Dependency files
# That -MMD and -MP flags together to generate Makefiles
# That generated Makefiles will take .o as .d to the output
CPP_FLAGS := $(INC_FLAGS) -MMD -MP
# Link: Generate executable file from object file
$(BUILD_DIR)/$(TARGET):$(OBJS)
$(CXX) -o $(TARGET) $(OBJS)
# Compile: Generate object files from source files
$(BUILD_DIR)/%.cpp.o: $(SRC_DIRS)/%.cpp
mkdir -p $(dir $@)
$(CXX) $(CPP_FLAGS) $(CXX_FLAGS) -c $< -o $@
.PHONY: clean
clean:
# rm -r $(BUILD_DIR)
rm -r $(OBJS) $(TARGET) *.d
-include $(DEPS)
{
"files.defaultLanguage": "c++",
"editor.suggest.snippetsPreventQuickSuggestions": false,
"editor.acceptSuggestionOnEnter": "off",
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
// 0.Only One Simple C/C++ file build, compile, debug...
//"c": "cd $dir && gcc -std=c11 -stdlib=libc++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
//"cpp": "cd $dir && g++ -std=c++11 -stdlib=libc++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
//"c": "cd $dir && make && ./fileNameWithoutExt && make clean",
//"cpp": "cd $dir && make && ./fileNmaeWithoutExt && make clean"
// "c": "cd $dir && make main && ../build/main && make clean",
// "cpp": "cd $dir && make main && ../build/main && make clean"
// 1.0 Reference
// Thanks chinese website blogger!
// (Configure multiple c file compilation with vscode on Mac os)
// https://blog.csdn.net/songjun007/article/details/117641162
//"c": "cd $dir && gcc -std=c11 -stdlib=libc++ -I ${workspaceFolder}/inc ${workspaceFolder}/*.c -o ${workspaceFolder}/build/${fileBasenameNoExtension} && $dir$fileNameWithoutExt",
//"cpp": "cd $dir && g++ -std=c++11 -stdlib=libc++ -I ${workspaceFolder}/inc ${workspaceFolder}/*.cpp -o ${workspaceFolder}/build/${fileBasenameNoExtension} && $dir$fileNameWithoutExt"
// 1.1Use gcc or g++
// "c": "cd $dir && gcc -std=c11 -stdlib=libc++ $dir/../src/*.c -o $dir/../build/$fileNameWithoutExt && $dir/../build/$fileNameWithoutExt",
//"cpp": "cd $dir && g++ -std=c++11 -stdlib=libc++ $dir/../src/*.cpp -o $dir/../build/$fileNameWithoutExt && $dir/../build/$fileNameWithoutExt"
//
// clang -g /Users/marryme/VSCode/CppProject/fitBody/src/bank.cpp /Users/marryme/VSCode/CppProject/fitBody/src/main.cpp -o /Users/marryme/VSCode/CppProject/fitBody/build/main
// 1.2Use clang or clang++
//"c": "cd $dir && clang -std=c11 -stdlib=libc++ $dir/../src/*.c -o $dir/../build/$fileNameWithoutExt && $dir/../build/$fileNameWithoutExt",
//"cpp": "cd $dir && clang++ -std=c++11 -stdlib=libc++ $dir/../src/*.cpp -o $dir/../build/$fileNameWithoutExt && $dir/../build/$fileNameWithoutExt"
// 2.Seprated multiple sourece C/C++ files under different folder build, compile, debug...
// if put main.o and debug folder into new directory ./build/
//"c": "cd $dir && clang -std=c11 -stdlib=libc++ $dir/*.c -o $dir/../build/$fileNameWithoutExt && $dir/../build/$fileNameWithoutExt",
//"cpp": "cd $dir && clang++ -std=c++11 -stdlib=libc++ $dir/*.cpp -o $dir/../build/$fileNameWithoutExt && $dir/../build/$fileNameWithoutExt"
// 3.Mutiple sourece C/C++ files under same folder build, compile, debug...
// if put main.o and debug folder into the current directory "./"
// No Makefile settings.json
"c": "cd $dir && clang -std=c11 -stdlib=libc++ $dir/*.c -o $dir/$fileNameWithoutExt && $dir/$fileNameWithoutExt", // Have changed
"cpp": "cd $dir && clang++ -std=c++11 -stdlib=libc++ $dir/*.cpp -o $dir/$fileNameWithoutExt && $dir/$fileNameWithoutExt" // Have changed
// Makefile settings.json
// "c": "cd $dir && make && $dir/$fileNameWithoutExt && make clean",
// "cpp": "cd $dir && make && $dir/$fileNameWithoutExt && make clean"
},
"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"
],
"C_Cpp.updateChannel": "Insiders",
"[makefile]": {
"editor.insertSpaces": true
},
"C_Cpp.default.includePath": [
"${workspaceFolder}"
],
"clang.completion.enable": true
}
{
"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
}
}
]
}
#include <iostream>
#include "test.h"
using namespace std;
int main()
{
cout << testCalculation(5, 7) << endl;
cout << endl;
return 0;
}
#ifndef test_h
#define test_h
int testCalculation(int, int);
#endif /* test_h */
#include "test.h"
int testCalculation(int x, int y) {
int sum = int();
sum = x + y;
return sum;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment