Trying to run separated classes in vs code in c++ [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 months ago.
I am new to vs code, after previously using atom a little bit. I am working on c++ with a program that has some classes in separated files. I have already installed the c/c++ extension, but I am facing a problem. When I try to run the program with all it's files included, it shows me the following error:
main.cpp:(.text+0x1f): undefined reference to `Car::sayHello()'
collect2: error: ld returned 1 exit status.
the code is pretty basic.
#include <iostream>
#include "Car.h"
using namespace std;
int main(){
Car c;
c.sayHello();
cout << "Hello world";
return 0;
}
From what I remember from atom, selecting all the files and running them would solve the problem, which I think is the same as running the following command in the terminal:
g++ Car.cpp Car.h main.cpp -o Classes
I have tried these in vs code and the problem seems to appear again. Help is much appreciated.

So if somebody else might have the same problem as me in the future. The problem was that I had declared the constructor and the destructor in the header file, but I hadn't defined them in the .cpp file, after doing that the program seems to be running well. Also as Fred mentioned Car.h is redundant to be mentioned in compilation process, but anyways the program will compile successfully, even if it is used.

So I had the same problem but I figured it out thanks to this link. Follow the instructions and test it out and it should work. There are also other ways to solve this problem based on your preference that are shown on this website. I did notice, however, that once you add multiple folders to your workspace, compiling whatever main.exe file you want becomes difficult since you use the arg ${workspaceFolder}\\*.cpp to compile all the .cpp files in your workspace folder. One way to solve that is to create multiple workspaces, but if anyone knows a more efficient way to do this, please let me know.

you can install fleeox in VS code that will help

Related

Undefined reference to linker error when using namespaces in headers in c++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I've read through all similar "Undefined reference to" threads I could find but couldn't find a solution. Most of the other threads also involved classes which I don't plan to use for this. The program compiles executes normally if I define the function within the header instead of using an external .cc file. I feel like I'm missing something simple here.
This is the simplest test I could put together that recreates the issue I'm having.
Compiler: g++ (Debian 8.3.0-6) 8.3.0
hntest.h
namespace hntest
{
void pewpew();
}
hntest.cc
#include <iostream>
#include "hntest.h"
namespace hntest
{
void pewpew()
{
std::cout << "pew pew pew!!!" << std::endl;
}
}
hntestmain.cc
#include "hntest.h"
int main(int argc, char* argv[])
{
hntest::pewpew();
}
I'm attempting to compile with:
g++ -lstdc++ hntestmain.cc -o hntestmain
And I get the following linker error:
hntestmain.cc:(.text+0x10): undefined reference to `hntest::pewpew()'
collect2: error: ld returned 1 exit status
I have tried reading through the code of a couple popular C++ libraries as well as some of my own older C (not ++) code and makefiles but haven't been able to find my error. I'm admittedly both and amateur an a bit rusty.
What am I missing?
You are not actually compiling the cpp file that has the definition of pewpew.
Try:
g++ -lstdc++ hntestmain.cc hntest.cc -o hntestmain
The compiler needs to know about all the source files. The header file is dealt with during pre-process and knows to look in the same folder. You can imagine that if you have more files, say 10, 100 or 10000, this would become impossible to manage by using command line. That is why people created build systems like make, cmake and bazel.
For much greater detail see the answer here which is specific for your case of linker error.

How to resolve the undefined reference error to readimagefile in dev C++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am getting an error in Dev C++ with the function readimagefile.
It says 'undefined reference to readimagefile' whenever I try to use the function included in the header file 'graphics.h'. Here is my code to show an image:
int main()
{
initwindow(600,600,"Trial");
readimagefile("alpha.jpg",0,0,500,500);
getch();
return 0;
}
It would be really helpful if someone could provide me with its correction or a substitute code to show an image.
Also if I input this image, would I be able to use this as a background? If not then what is the best way to import an image as a background in C++.
I have seen many people asking for help about this and fair enough, these errors doesn't show up on other compilers like code blocks or visual studio but I have to use Dev C++ so please make sure that your code is applicable in Dev C++.
I try to use the function included in the header file 'graphics.h'
Assuming you posted whole file - you need to add #include "graphics.h" at the top of your cpp file if you want to use functions from it.
Also, it seems that you are trying to use WinBGIm - don't forget checking if you did every step from their installation guide - especially the part about needed linker commands:
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32

The procedure entry point _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1Ev could not be located in the dynamic link library [duplicate]

This question already has answers here:
the procedure entry point __gxx_personality_v0 could not be located
(5 answers)
Closed 3 years ago.
Lately I have been trying to store strings in variables. I did quite a few searches online, and most of the answers that I have found recommend the use of std::string. So I've written a test program that looks like this:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
getline(cin,s);
cout << s;
}
The program compiles fine, however, when I run the program I revieve this error:`Error Message
I am a huge beginner to programming and confused on what is going on. Can anyone please give me some insight on what is happening here? Thanks!
Also, I am using the GCC compiler.
EDIT: I have successfully solved the issue. All I did was reinstall GCC using a tutorial on the web. I do not know what was wrong, but it is fixed now.
When you use GCC to compile C++, you need to use the g++ driver program, not gcc. The latter will compile the translation units, but it will not link the executable correctly.

Undefined Reference when Compiling Kobuki ROS examples from source

I have been trying to compile the kobuki_keyop example in ROS Indigo from source on Ubuntu 14.04, I basically wrote a simple cpp file like the following:
#include "/opt/ros/indigo/include/kobuki_keyop/include/keyop_core/keyop_core.hpp"
using namespace keyop_core;
int main()
{
KeyOpCore keyy;
}
then I compiled with the following:
g++ test.cpp -L/opt/ros/indigo/lib/kobuki_keyop -o test
but it keeps giving me the same old holy grail of cpp errors:
/tmp/ccsh6f87.o: In function `main':
test.cpp:(.text+0x25): undefined reference to `keyop_core::KeyOpCore::KeyOpCore()'
test.cpp:(.text+0x34): undefined reference to `keyop_core::KeyOpCore::~KeyOpCore()'
Update:
as was pointed out by #Danh, there are solutions in the following link:
What is an undefined reference/unresolved external symbol error and how do I fix it?
however, my question is ROS related, I did not write the header files that might cause the problem, and I can't poke around all of the interlinked ones hoping to find a needle in the haystack, I was just hoping someone with background in ROS who had a similar problem might pick this up and point me to their solution.
Thank You.

expected type-specifier and cannot convert ‘int*’ in initialization

I have a large amount of code, so I have tried to only include the relevant parts of the code here
My cpp files compiled with no problems when they were included in another cpp file.
I have another file called. This compiled fine before, until I tried to include the files above in
I get undefined reference errors, even though they were defined in the cpp file
What is going on? Is there a linkage error? Do I need to make changes in the makefile?
Using
ASp* asp = new ASp(input);
works for me. Not sure why using
ASp* asp = new ASp::ASp(input);
is a problem. Need to dig further to find out.
Update
The same problem was addressed at using declarations in main (C++). The tool chain for that question is MS Visual Studio. The answers there point out the same problem but there is no explanation of why new Asp::Asp(input); would be of type int*. The error message reported by the OP appears to be a g++ specific error message.