0

I'm trying to use Visual Studio for a specific project but I can't get files to link properly. When including a header file with a defined function in another cpp file im getting an error undefined reference to testFunc() collect2.exe: error: ld returned 1 exit status

Thing is, this exact same code works perfectly in Eclipse. Can someone tell me what I'm doing wrong?

Test.cpp

#include "Other.h"

int main(){
   
    testFunc();
    return 0;
}

Other.h

#pragma once

void testFunc();

Other.cpp

#include "Other.h"
#include <iostream>
using namespace std;

void testFunc(){
    cout << "HelloWorld";
}

When Buildung, this occours:

Starting build...
C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g C:\Users\johan\cu-workspace\TEst\Test.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe
C:\Users\johan\AppData\Local\Temp\cck3aAZo.o: In function `main':
C:/Users/johan/cu-workspace/TEst/Test.cpp:5: undefined reference to `testFunc()'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).
273K
  • 29,503
  • 10
  • 41
  • 64
  • 1
    Have you added `other.cpp` to the project? Please show the build commands and its output. – Quimby Jan 05 '23 at 14:19
  • Im Sorry I was talking about Visual Studio Cude. All My files are in the same folder and the launch.json references to this folder "cwd": "c:/Users/johan/cu-workspace/TEst" – Johannes K. Jan 05 '23 at 14:32
  • 1
    Well, the build log clearly show you are not building `other.cpp`, so add it to that command. – Quimby Jan 05 '23 at 14:41
  • if you use more than 1 cpp file you have to use a build tool like Makefile or CMake, soon you will have way more than 5 cpp files and then the build phase will take too long and you will complain about that, also maintain the build in launch.json is a nightmare for big CPP projects – rioV8 Jan 05 '23 at 17:33

2 Answers2

0

If you build Other.h and Other.cpp as a project, then you need to configure the linker to add Other.lib into test project.
For a simple scenario, you can have all 3 files in one project and they should build just fine.

Ionut V.
  • 110
  • 7
0

According to your build info:

C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g C:\Users\johan\cu-workspace\TEst\Test.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe

You can see that Other.cpp is not in your project, so you might need to add it into your project.

Since you are using VS code, you can write a simple command in terminal to build your code:

C:\MinGW\bin\g++.exe -g C:\Users\johan\cu-workspace\TEst\Test.cpp C:\Users\johan\cu-workspace\TEst\Other.cpp -o C:\Users\johan\cu-workspace\TEst\Test.exe
LiuYuan
  • 127
  • 9
  • Using that seems to work, yet only for building. When I want to run the code inside of VSCode to use the terminal I'm still running into the same problem. I also got to work by making a few changes in the task.json, but also just for building. Is there a way so tell VSCode to run the code via your command or including the task.json? – Johannes K. Jan 05 '23 at 16:52
  • @JohannesK. Could you explain more about *I want to run the code inside of VSCode to use the terminal*? I didn't get the point. Since the code you provided is not very complicated, I suggest you can run the executable file in terminal with command `./Test.exe` after you build your code successfully. By the way, if you just start learning coding, I suggest to use Visual Studio for it can reduce the hassle of configuring your environment. You should spend your energy on the code itself, not on the environment configuration. – LiuYuan Jan 06 '23 at 04:36