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.
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?
Related
hope you guys are doing well. I am just getting linker error in C++ , I don't know why? Everything is correct....
Check below testing.h file
#ifndef __MYClass__
#define __MYClass__
#include<iostream>
using namespace std;
class Abc {
private:
int a;
public:
void input();
void display();
};
#endif
and here's implementation of these functions in Functions.cpp file.
#include"testing.h"
void Abc::input() {
cout<<"Enter any value : ";
cin>>a;
}
void Abc::display() {
cout<<"You Entered : "<<a;
}
And now, in main.cpp
#include<iostream>
#include"testing.h"
using namespace std;
int main() {
Abc obj;
obj.input();
obj.display();
return 0;
}
All files are compiled successfully.
In main.cpp Linker says....
g++ -Wall -o "main" "main.cpp" (in directory: /home/Welcome/C++ Practices/testingLinux)
/usr/bin/ld: /tmp/ccYI9LAy.o: in function main': main.cpp:(.text+0x10): undefined reference to Abc::input()'
/usr/bin/ld: main.cpp:(.text+0x1c): undefined reference to `Abc::display()'
collect2: error: ld returned 1 exit status
Compilation failed.
I'm using built-in linux compiler...
There are multiple ways you can fix this but before that please read up on Translation Unit.
Coming to your problem.
When you write
g++ -Wall -o main main.cpp
The compiler will pick up main.cpp for compilation and expand testing.h that includes the declaration for class ABC and with this header file it can determine what is the size of ABC and be able to generate instructions reserving space for obj on the stack. It can't see the definition for input() and display() hence defers that task to the linker. Note that testing.cpp is not in the picture at all since the compiler doesn't know that the implementation of ABC is in testing.cpp. Now when the linker tries to resolve the symbols input() it fails to find the definition for it and throws the error
undefined reference to Abc::input()
So, to fix this you can tell explicitly upfront that it also needs to take in testing.cpp while compiling main.cpp by
g++ -o main main.cpp testing.cpp
Another way is to create a dynamic library out of testing.h and testing.cpp
g++ -shared -fPIC testing.cpp -o libtest
and then link it against main.cpp
g++ -o main main.cpp -I. -L. libtest
What this does is that the compiler still can't figure out the definition of input() and display() but the linker can since now the library containing the definitions is provided to it.
You are not compiling Functions.cpp file.
This should fix your issue:
g++ main.cpp Functions.cpp
I'm going crazy for this problem. I downloaded the source code and pre-compiled files of sqlite3 according to the tutorial, and used vs to compile, got the sqlite3.lib and sqlite3.exp files, and put it in the C: \ sql folder. Then I created the project in vscode, and put sqlite3.h in the root directory of the project, and created the following test code.
#include "sqlite3.h"
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
#pragma comment(lib, "c:\\sql\\sqlite3.lib")
int main() {
sqlite3 *db = NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("Student.db", &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
} else
printf("open mydata successfully!\n");
sqlite3_close(db);
system("pause");
return 0;
}
My tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "gdb",
"type": "shell",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-LC:\\sql",
// "${fileDirname}\\src\\Account.cpp",
// "${fileDirname}\\src\\Person.cpp",
// "${fileDirname}\\src\\Date.cpp",
// "${fileDirname}\\src\\System.cpp",
// "${fileDirname}\\src\\Administrator.cpp",
"-lsqlite3",
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
But no matter how I modify the tasks.json file, I still get the following result. I am really too lost. Can anyone help me how to solve this problem?
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Drew\AppData\Local\Temp\ccUXavQh.o:test.cpp:(.text+0x2e): undefined reference to `sqlite3_open'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Drew\AppData\Local\Temp\ccUXavQh.o:test.cpp:(.text+0x45): undefined reference to `sqlite3_errmsg'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Drew\AppData\Local\Temp\ccUXavQh.o:test.cpp:(.text+0x6d): undefined reference to `sqlite3_close'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\Drew\AppData\Local\Temp\ccUXavQh.o:test.cpp:(.text+0x91): undefined reference to `sqlite3_close'
collect2.exe: error: ld returned 1 exit status
I personally download the sqlite-amalgamation source and compile it as a regular source file. SQLite made it very easy and you should have no problem https://www.sqlite.org/download.html
I don't mess with static/dynamic libs when I don't need to. The problem may occur because of which compiler compiled the library (ie ms compiler, mingw, clang, etc). There's a chance you may be mixing them causing the error. I suspect the precompile one is using ms compiler. From memory clang works with libs compiled with ms compiler (clang depends on it). mingw doesn't depend on it and won't use ms compiled libs
I'm taking an intro class into C++ and we are using jgrasp. I'm doing a simple exercise right now which looks like this:
#include <iostream>
using namespace std;
int main()
{
cout << "Testing 1, 2, 3\n";
return 0;
}
though when I compile it and link it I get this error:
----jGRASP exec: g++ -c -fsyntax-only H:\COP2334\Excerises\Display 1.10
g++.exe: warning: H:\COP2334\Excerises\Display 1.10: linker input file unused because linking not done
----jGRASP: operation complete.
The option -c means "Compile or assemble the source files, but do not link."
So, of course, linking isn't performed.
I moved from Windows to Ubuntu and I wanted to try some C++ programming on Ubuntu. So here is very simple code and very stupid error which I can't resolve:
horse.h
#ifndef _horse_
#define _horse_
class Horse{
int speed;
public:
void saySomething();
};
#endif
horse.cpp
#include "horse.h"
#include <iostream>
using namespace std;
void Horse::saySomething(){
cout << "iiiihaaaaaaa brrrrr."<<endl;
}
and Main.cpp
#include "horse.h"
int main(){
Horse h;
h.saySomething();
}
After I compile (compilation is successful) and run this I get this error message:
/tmp/ccxuDyrd.o: In function `main':
Main.cpp:(.text+0x11): undefined reference to `Horse::saySomething()'
collect2: ld returned 1 exit status
Please help me somehow.
Try
g++ -c main.cpp horse.cpp (to compile)
g++ -o a.out main.o horse.o (to link)
It seems you only compiled your code but did not link the resulting object files. You probably invoked the compiler like this:
g++ main.cpp
You should instead compile every *.cpp file separately and then link each resulting *.o file. And you should do this with a Makefile.
Actually, the basic idea is the same on Windows with MSVC. The compiler produces object files, the linker links them together.
When I run and build a simple program, it fails.
Here is the error message:
g++ -Wall -o "main" "main.cpp" (in directory: /home/markuz/Desktop)
/tmp/ccHV9wPu.o: In function main':
main.cpp:(.text+0x11): undefined reference toTest::display()'
collect2: ld returned 1 exit status
Compilation failed.
Here are the files. The compile and build command is the default of geany 1.22
//main.cpp
#include "imba.h"
int main(){
Test t;
t.display();
return 0;
}
//imba.h
class Test{
public:
void display();
};
//imba.cpp
#include <iostream>
#include "imba.h"
void Test::display(){
std::cout << "oi";
}
Any ideas about this?
Thanks.
You need to also add the imba.cpp file in the compilation step. Although you have included the header in your main file, you have not compiled the source for it and so the linker cannot find the object file for imba.cpp - that is what the error is complaining about