Skip to content

Instantly share code, notes, and snippets.

@SofijaErkin
Last active March 22, 2022 02:36
Show Gist options
  • Save SofijaErkin/3876ad0d11f0a9749b1ca1ef833be44c to your computer and use it in GitHub Desktop.
Save SofijaErkin/3876ad0d11f0a9749b1ca1ef833be44c to your computer and use it in GitHub Desktop.
Common seperated from C/C++ main folder compile, debug using cmake at vscode on mac.

Common Seperated Apps C/C++ Manually CMake VSCode on Mac

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

The Architecture of workspace are:

mainSub

|

|_.vscode

|     |__c_cpp_properties.json

|     |__launch.json

|     |__settings.json

|     |__tasks.json

|__CMakeLists.txt

|_build

|     |_debug

|     |_release

|_apps

|     |__main.cpp

|_common
      
|      |_include
      
|      |     |__Pet.h
      
|      |_source

|            |__Pet.cpp

|__comsepeapp_cmake_compile_problem.md

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}/common/include/**" // Have Changed! That was external 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...3.22 FATAL_ERROR)
# CMake Minimum Required Version :
project(com_Sepe_App
VERSION
0.1.0
DESCRIPTION
"A project to test comSepeApp build, compile"
LANGUAGES
CXX)
set(CMAKE_CXX_STANDARD 11)
message(WARNING CMAKE_CXX_STANDARD="${CMAKE_CXX_STANDARD}")
# set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS OFF)
message(WARNING CMAKE_CXX_EXTENSIONS="${CMAKE_CXX_EXTENSIONS}")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(WARNING CMAKE_CXX_STANDARD_REQUIRED="${CMAKE_CXX_STANDARD_REQUIRED}")
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
message(WARNING CMAKE_ARCHIVE_OUTPUT_DIRECTORY="${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
message(WARNING CMAKE_LIBRARY_OUTPUT_DIRECTORY="${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
message(WARNING CMAKE_RUNTIME_OUTPUT_DIRECTORY="${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
# Notice: this three command need to be changed for mac!
# Control where the static and shared libraries are built so that on windows,
# we don't need to tinker with the path to run the executable.
include_directories(${CMAKE_CURRENT_LIST_DIR}/common/include/*.h) # Notice: have changed!
set(SOURCES apps/main.cpp common/source/Pet.cpp) # Notice: have changed!
include(CTest)
enable_testing()
add_executable(main ${SOURCES})
# add_executable(main main.cpp)
# contains an petActive we will use to
# add_subdirectory(petActive)
# target_link_libraries(main
# PRIVATE
# petActive
# )
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

The problem happens comSepeApp Manually Compile using CMake

1.Problem A

If top CMakeList.txt includes:

include(GNUInstallDirs)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY

   ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY

   ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY

   ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

Also, some changes in lanuch.json and settings.json. Target file is located at

from ~/build/debug to ~/build/debug/bin.

1.1Terminal Output During Compile

cmake:

-- Configuring done

-- Generating done

-- Build files have been written to: /Users/yq/VSCode/CppProject/comSepeApp/build/debug

make:

[ 66%] Building CXX object CMakeFiles/main.dir/common/source/Pet.cpp.o

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

[100%] Linking CXX executable bin/main

[100%] Built target main

1.2Run Or Not

Run.

1.3Fix Or Not

No need to fix.

1.4May be

That controls the storing directory of the target file.

1.5Others Fix Or Not

No.

2.Problem A

If top CMakeList.txt Not includes:

include(GNUInstallDirs)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY

   ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY

   ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY

   ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

Also, some changes in lanuch.json and settings.json. Target file is located at

from ~/build/debug/bin to ~/build/debug.

2.1Terminal Output During Compile

cmake:

-- Configuring done

-- Generating done

-- Build files have been written to: /Users/marryme/VSCode/CppProject/comSepeApp/build/debug

make:

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

[ 66%] Building CXX object CMakeFiles/main.dir/common/source/Pet.cpp.o

[100%] Linking CXX executable main

[100%] Built target main

2.2Run Or Not

Run.

2.3Fix Or Not

No need to fix.

2.4May be

That controls the storing directory of the target file.

2.5Others Fix Or Not

Using the generated target way of the acceleator mapping "cmake" and

"make" debug more faster than "Run Code ^\N".

{
// 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/bin/${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 "../common/include/Pet.h" // Have changed
#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" // Have changed
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 && $dir../build/debug/bin/$fileNameWithoutExt", // Notice: have changed!
"cpp": "cd $dir && cd ../build/debug && cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ../.. && make -j 8 && $dir../build/debug/bin/$fileNameWithoutExt" // Notice: have changed!
},
"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 ../..",// Have changed
"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"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment