Skip to content

Instantly share code, notes, and snippets.

@SofijaErkin
Created March 22, 2022 06:09
Show Gist options
  • Save SofijaErkin/5b6b655aa6ce3011657560b80ed2c06f to your computer and use it in GitHub Desktop.
Save SofijaErkin/5b6b655aa6ce3011657560b80ed2c06f to your computer and use it in GitHub Desktop.
(Tween CMake Version)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(Tween CMake Version)

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

|_apps

|     |__CMakeLists.txt

|     |__main.cpp

|_build

|     |_debug

|     |_release

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

|      |     |__Pet.cpp

|      |__CMakeLists.txt

|__CMakeLists.txt

|__comsepeapp_cmake_compile_problem.md

I will add the 9 file via adding file except the subdirectory CMakeLists.

CMakeLists.txt under ~/apps/CMakeLists.txt

# using main.cpp to product main.cpp.o, then using main.cpp.o to generate 

# target main

add_executable(main main.cpp)

# add sub directory class

# add_subdirectory(sub_directoy)

# target file to link sub_directory

# target_link_libraries(main

#  PRIVATE

#    sub_directory_A

#    sub_directory_B

#    ...

#    apposed_directory_A

#    apposed_directory_B

#    ...

#    )

target_link_libraries(main

  PRIVATE

    petactive
)

CMakeLists.txt under ~/common/CMakeLists.txt

add_library(petactive "")

target_sources(petactive

PRIVATE

  ${CMAKE_CURRENT_LIST_DIR}/source/Pet.cpp

PUBLIC

  ${CMAKE_CURRENT_LIST_DIR}/include/Pet.h
)

target_include_directories(petactive

  PUBLIC

    ${CMAKE_CURRENT_LIST_DIR}
)
{
"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
# Reference: https://stackoverflow.com/a/68360488/10846570
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!
# define targets and source
add_subdirectory(apps)
# contains an "petActive" common class we will use to
add_subdirectory(common)
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)
{
// 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
}

The problem happens comSepeApp Manually Compile using CMake

1.Problem A

1.1Terminal Output During Compile

-- Configuring done

CMake Error at apps/CMakeLists.txt:3 (add_executable):

  Cannot find source file:

    /Users/marryme/VSCode/CppProject/subMainClass/common/include/Pet.cpp

CMake Error at common/CMakeLists.txt:1 (add_library):

  Cannot find source file:

    /Users/marryme/VSCode/CppProject/subMainClass/common/include/Pet.cpp

-- Generating done

1.2Run Or Not

Not.

1.3Fix Or Not

Fix.

Trouble has happened on CMakeLists.txt under "~/common/CMakeLists.txt".

Change from

target_sources(petactive

  PRIVATE
    
    ${CMAKE_CURRENT_LIST_DIR}/source/Pet.cpp

  PUBLIC

    ${CMAKE_CURRENT_LIST_DIR}/include/Pet.cpp
)

to

target_sources(petactive

  PRIVATE
    
    ${CMAKE_CURRENT_LIST_DIR}/source/Pet.cpp

  PUBLIC

    ${CMAKE_CURRENT_LIST_DIR}/include/Pet.h
)

1.4May be

Include file has troubled at it's exten-name.

1.5Others Fix Or Not

No.

{
// 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