Skip to content

Instantly share code, notes, and snippets.

@SofijaErkin
Last active March 15, 2022 07:53
Show Gist options
  • Save SofijaErkin/b683e96370ba18fd0eab30bdf733e480 to your computer and use it in GitHub Desktop.
Save SofijaErkin/b683e96370ba18fd0eab30bdf733e480 to your computer and use it in GitHub Desktop.

Problem Occur Setting-Up CMake in VSCode on mac

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

{
// Reference: https://medium.com/@dexterchan_44737/visual-studio-code-build-and-debug-a-c-with-cmake-on-mac-os-7633bc59bf34
"configurations": [
{
"name": "Mac",
"includePath": [
// The include files directory path.
// "${workspaceFolder}/**"
// has Changed!
// "${workspaceFolder}",
"${workspaceFolder}/include", // 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": [
// This is the FrameworkPath of Mac Now we are using.
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks",
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks",
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "clang-x64",
// This time we use MicroSoft CMake-Tools.
"configurationProvider": "vector-of-bool.cmake-tools",
// This is to Compile the main Cpp.
// Or generate object files from source files.
// Also, is the same as "code-runner.executorMap" settings.json
// during using Code-Runner Module.
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"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(HelloWorld ${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",
"type": "cppdbg",
"request": "launch",
// "program" is the target file diretory
// "program": "${fileDirname}/${fileBasenameNoExtension}",
"program": "${workspaceFolder}/${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" // ?
}
]
}
// 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 "include/Pet.h"
#include <iostream>
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 <string>
class Pet{
private:std::string name;
public:Pet(std::string& name);
void sound();
void eat();
std::string& getName();
};
{
"files.associations": {
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__functional_base": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__tuple": "cpp",
"algorithm": "cpp",
"atomic": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"complex": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"locale": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"*.tcc": "cpp",
"cinttypes": "cpp"
},
"clang.cxxflags": [
"-std=c++11"
],
"clang.cflags": [
"-std=c11"
]
}
{
// 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}" // 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.
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment