Reputation: 41
Normally I use VS19 for C++ programming, but I wanted to try if it works on VSCode on my Macbook, so I wrote a real simple program:
main
#include <iostream>
#include "test.hpp"
using namespace std;
int main()
{
testfile obj(5);
cout << "main.cpp" << endl;
obj.output();
return 0;
}
class header (.hpp)
#pragma once
using namespace std;
class testfile
{
private:
int i;
public:
testfile(int in) : i(in) {}
void output();
};
class file (.cpp)
#include "test.hpp"
#include <iostream>
using namespace std;
void testfile::output()
{
cout << i << endl;
}
I know I could write the little output in the header, but I want to try if it works if the code is split up into many different files. I get the following error:
(PATH)..\Temp\ccITg6NM.o:main.cpp:(.text+0x48): undefined reference to `testfile::output()' collect2.exe: error: ld returned 1 exit status
The same goes for my windows laptop. I ran the exact same code on Visual Studio and it worked perfectly fine. I tried googling the error but tbh, I didn't got anything out of it...
I run VSCode with C/C++ intellisense and the compile & run plugin.
Upvotes: 4
Views: 14208
Reputation: 95
It's pretty tricky... I'm not sure but the linker maybe ignore the implementation of testfile::output()
in test.cpp
because the header test.hpp
include implementation of constructor testfile::testfile(int in)
.
I actually cannot reproduce the problem, try this :
test.hpp
#pragma once
using namespace std;
class testfile
{
private:
int i;
public:
testfile(int in);
void output();
};
test.cpp
#include "test.hpp"
#include <iostream>
using namespace std;
testfile::testfile(int in) : i(in) {}
void testfile::output()
{
cout << i << endl;
}
I think it is better that all implementation are in *.cpp file like above.
EDIT :
I use g++ for compiling those files(I'm sorry I don't have VScode environment).
command line(correct) : g++ main.cpp test.cpp -o out
output :
D:\workspace\test2\test2>out
main.cpp
5
command line(incorrect, test.cpp is missing) : g++ main.cpp -o out
output :
${User}\AppData\Local\Temp\ccYKJ92L.o:main.cpp:(.text+0x1a): undefined reference to `testfile::testfile(int)'
${User}\AppData\Local\Temp\ccYKJ92L.o:main.cpp:(.text+0x48): undefined reference to `testfile::output()'
collect2.exe: error: ld returned 1 exit status
command line(incorrect, main.cpp is missing) : g++ test.cpp -o out
output :
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
EDIT2 :
I installed VSCode although I use windows, I think I figured out why these type of error occur.
You might use F6 command for build the sources w/ C/C++ Compile Run, but the F6 command is applied only for a file which is currently selected and showed on editor. You select main.cpp then linker cannot find method of class testfile
, otherwise select test.cpp then linker cannot find entry point(main) of project.
So if you want to build correctly, you must make kind of build script(makefile, json, something).
If you type Ctrl+Shift+P, you can fild Tasks: Configure task
tab. Click them and configure your setting(host OS, compiler, ...) It would gives you default tasks.json
file with minimal form.
I use this json file(Windows, mingw(gcc for windows))
tasks.json
{
"version": "2.0.0",
"command": "g++",
// compiles and links with debugger information
"args": ["-g", "-o", "out.exe", "main.cpp", "test.cpp"],
}
Ctrl+Shift+B (build using above json), then its output:
running command> g++ -g -o out.exe main.cpp test.cpp
It builds out.exe
file successfully.
If you want to debug or run, use F5 or Ctrl+F5 or Debug tab on the top menu bar, then its output on debug console:
main.cpp
5
Debug or run step refers to launch.json file like build step refers to tasks.json file.
for reference, launch.json :
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows)build test",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/out.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false
}
]
}
Concolusionally it's not problem of source. For further infomation refer to this post :
How do I set up Visual Studio Code to compile C++ code?
Upvotes: 3