Skip to content

Instantly share code, notes, and snippets.

@SofijaErkin
Last active March 22, 2022 03:30
Show Gist options
  • Save SofijaErkin/f2a5f076b6c70accd2432c94b76ef734 to your computer and use it in GitHub Desktop.
Save SofijaErkin/f2a5f076b6c70accd2432c94b76ef734 to your computer and use it in GitHub Desktop.
(Tween CMake Version)Libclass seperated from C/C++ compile, debug using cmake at vscode on mac.

Seperated Libclass 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

|__CMakeLists.txt

|_build

|     |_debug

|     |_release

|__main.cpp

|__mainSub_cmake_compile_problem.md

|_petActive

      |__CMakeLists.txt
      
      |_include
      
      |     |__Pet.h
      
      |_source

            |__Pet.cpp

I will add the 10 file via adding file.

Others,

~/mainSub/petActive/CMakeLists.txt just like:

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

Reference.

{
"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}/petActive/include/**" // Have Changed
],
"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)
# Reference: https://stackoverflow.com/a/68360488/10846570
# CMake Minimum Required Version :
project(main_Sub_Test
VERSION
0.1.0
DESCRIPTION
"A project to test subCMake 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}/pet/*.h) # Notice: have changed!
# set(SOURCES petCom.cpp pet/Pet.cpp) # Notice: have changed!
include(CTest)
enable_testing()
# add_executable(petCom ${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 "petActive/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();
}

The problem during Project mainSub manually compiling using CMake

Let's reinstate the environment of compiling problem!

1.Problem A

1.1Terminal output during compiling

CMake Error at petActive/CMakeLists.txt:3 (target_sources):

  Cannot specify sources for target "PRIVATE" which is not built by this

  project.


-- Configuring incomplete, errors occurred!

See also "/Users/marryme/VSCode/CppProject/mainSub/build/debug/CMakeFiles/CMakeOutput.log".

1.2Run Or Not

Not.

1.3Fix Or Not

Fix.

Add target"petActive" to "PRIVATE" under ~/petActive/CMakeLists.txt.

target_sources(petActive
  PRIVATE
    ${CMAKE_CURRENT_LIST_DIR}/source/Pet.cpp
  PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/include/Pet.h
)

1.4May be

Notice:

target_sources(<target>
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])

1.5Other Fix Or Not

New Fixed.

2.Problem B

2.1Terminal output during compiling

zsh: no such file or directory: /Users/yq/VSCode/CppProject/mainSub/build/debug/main

2.2Run Or Not

Not.

2.3Fix Or Not

Fix.

Generated target file main was under ~/build/debug/bin/main.

Firstly, change target address directory from "$dirbuild/debug/$fileNameWithoutExt"

to "$dirbuild/debug/bin/$fileNameWithoutExt":

As that shows in settings,json under ~/.vscode/settings.json,

"code-runner.executorMap": {

  "c": "cd $dir && cd build/debug && cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ../.. && make -j 8 && $dirbuild/debug/bin/$fileNameWithoutExt",

  "cpp": "cd $dir && cd build/debug && cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug ../.. && make -j 8 && $dirbuild/debug/bin/$fileNameWithoutExt"
},

Secondly, change "program" under ~/.vscode/lanuch.json from

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

to

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

OK.

2.4May be

That connect with top CMakeListst.txt,

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

2.5Other Fix Or Not

Notice:

Executing task: make -j 8 <

[ 25%] Building CXX object petActive/CMakeFiles/petActive.dir/source/Pet.cpp.o

[ 50%] Linking CXX static library ../lib/libpetActive.a

[ 50%] Built target petActive

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

[100%] Linking CXX executable bin/main

[100%] Built target main

Finished.

END

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