I am trying to run the debugger for a simple "hello world" like program but am running into linking errors.
I only have 3 files, Log.h
, Log.cpp
, Main.cpp
:
Log.h
#pragma once
void InitLog();
void Log(const char *);
Log.cpp
#include "Log.h"
#include <iostream>
void InitLog()
{
Log("Initializing Log");
}
void Log(const char *message)
{
std::cout << message << std::endl;
}
Main.cpp
#include "Log.h"
// #include "Log.cpp"
int main()
{
int var = 9;
char x = 'a';
Log("hello world!");
}
I changed my code-runner settings because I was getting a linker error that indicated the two functions Log()
and InitLog()
were declared but not defined.
Undefined symbols for architecture x86_64:
"Log(char const*)", referenced from:
_main in main-f30cc3.o
"InitLog()", referenced from:
_main in main-f30cc3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Switching code-runner settings to compile every file in the directory worked:
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
I mention this because potentially I have wired something incorrectly that I am unaware of (should I have even needed to update code runners functionality?)
While this works for compiling and linking the code, I am unable to debug the code because of the same linking error. My CPP/.vscode/launch.json
is as follows:
{
// 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": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: g++ build active file"
}
]
}
I can see that the same linking error occurs, and playing around with the program
and args
values did not work for me. My error is:
Terminal Error:
> Executing task: C/C++: g++ build active file <
Cannot build and debug because the active file is not a C or C++ source file.
The terminal process failed to launch (exit code: -1).
Terminal will be reused by tasks, press any key to close it.
There is a simple solution to this, I can just #include "Log.cpp" (the part I have commented) and the debugger works!
> Executing task: C/C++: g++ build active file <
Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g CPP/07Debugging/Main.cpp -o CPP/07Debugging/Main
Build finished successfully.
Terminal will be reused by tasks, press any key to close it.
However, I do not want to import the .cpp
files into my Main.cpp
everytime I need to debug - am I missing something here? Is there a way to compile and link all cpp files in my current directory in a way that I am unaware of? Searches for people running into the same debugger linker errors has proven fruitless, so I assume I am missing something obvious. I am running VSCode on Mac and compiling the c++ with g++
Aucun commentaire:
Enregistrer un commentaire