I am using vscode on ubuntu linux. I am trying to compile/debug the following 3 files that should be linked but I think I am getting a linker error indicating that the function files are not defined:
Main.cpp
#include <iostream>
void Log(const char* message);
int Multiply(int a, int b);
int main()
{
Log("Hello there! How old are you?");
std::cout << Multiply(2,3) << std::endl;
std::cin.get();
}
Log.cpp
#include <iostream>
void Log(const char* message)
{
std::cout << message << std::endl;
}
Math.cpp
#include <iostream>
int Multiply(int a, int b)
{
int result = a * b;
return result;
}
Please note that I declare the functions in the last two files (Log.cpp and Math.cpp) in Main.cpp. I however get the following error when I run the the code (using g++ debugger):
launch: program '~./Dev/Src/Main' does not exist
with the option to either Cancel
or Open launch.json
launch.json setup looks like this:
{
// 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": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json also looks like this:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The terminal gives the following error:
> Executing task: C/C++: g++ build active file <
Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g /Dev/src/Main.cpp -o /Dev/src/Main
/usr/bin/ld:
/tmp/cchjsd06.o: in function `main':
/Dev/src/Main.cpp:24: undefined reference to `Log(char const*)'
/usr/bin/ld: /Dev/src/Main.cpp:25: undefined reference to `Multiply(int, int)'
collect2: error: ld returned 1 exit status
Build finished with error(s).
Terminal will be reused by tasks, press any key to close it.
I cannot determine what exactly seems to be causing the linker error here. I am curious what I may be missing and would appreciate any insights.