3

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.

B. Go
  • 1,436
  • 4
  • 15
  • 22
marcopasta
  • 31
  • 1
  • 1
  • 2
  • The `testfile` source file is in the project? In the actual makefile (or other build system used)? How do you build? What are your build-settings? What does you makefile (or other build-system configuration file) do? – Some programmer dude Dec 17 '19 at 08:47
  • I don't have a makefile yet, i used the plugin to compile the code. I don't know how to make a makefile yet but i guess i should look into it. On my mac i tried with g++ on the terminal and i tried to include all 3 files but i always got an output failure.. – marcopasta Dec 17 '19 at 08:53

1 Answers1

3

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?

H.U.N.
  • 95
  • 9
  • Hi, i tried your method but i got stuck with the error: "undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status". I showed it to a prof and he told me to include the testfile.cpp into my main.cpp and it worked. But since its been working for you without including the testfile.cpp into the main, can i link it manually with the makefile? – marcopasta Dec 17 '19 at 11:30
  • You said you tried compiling with g++ and you got same errors, but I highly suspect that the errors you said are seemed to miss one of *.cpp file. Can you share your g++ command line and errors? – H.U.N. Dec 18 '19 at 04:37
  • Hi, as i stated, i compile all of my code with a plugin in VSCode, but after i tried your command on my mac terminal it all applied fine and there were no further issues. Thanks for that! The error i get from VSCode are the following: `C:\Users\LUCACH~1\AppData\Local\Temp\ccXvErUU.o:main.cpp:(.text+0x1a): undefined reference to testfile::testfile(int)` `C:\Users\LUCACH~1\AppData\Local\Temp\ccXvErUU.o:main.cpp:(.text+0x26): undefined reference to testfile::output() collect2.exe: error: ld returned 1 exit status` Meanwhile on the mac everything works fine. – marcopasta Dec 18 '19 at 19:26
  • Thanks for the command line pointers! – Bananeen Dec 29 '21 at 00:12