Trouble with running code in Visual Studio Code - c++

I know this has been asked many many times, but I am a complete beginner at Linux and I have never used it before this. I am trying to set up VSCode for the past hour or so, and now that I finally got the extensions working (did i?) I tried testing it out and it doesn't work. When I type couple basic lines of code:
#include <iostream>
using namespace std;
int main()
{
cout << "It's not working" << endl;
return 0;
}
And when I try to run the code I get this:
[Running] cd "/home/user/School/CPP Codes/" && g++ test.cpp -o test && "/home/user/School/CPP Codes/"test
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function _start':
(.text+0x20): undefined reference tomain'
collect2: error: ld returned 1 exit status
[Done] exited with code=1 in 0.096 seconds
I read on some of the posts that I should add -o ?! But again I have 0 clue what that means or what I should do exactly. So any help would be insanely appreciated!

Linker errors are indeed hard to debug. After some years you will get them a bit better. Basically the linker tells you, that he finds the file, but does not find your main. As i mentioned in my comment this could be
you did not safe the file
your file got buffer in any way
In my experience, vs code shows the status of saving a bit hidden. where the cross for closing the file is, there is a round dot.

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 do I interpret and resolve "dyld: Symbol not found ..." [duplicate]

After compiling, I am trying to run libuv sample program:
#include <stdio.h>
#include <uv.h>
int main() {
uv_loop_t *loop = uv_loop_new();
printf("Now quitting.\n");
uv_run(loop, UV_RUN_DEFAULT);
return 0;
}
But, when try to run, I get the following error:
**/tmp/ccHTpspB.o: In function `main':
main.c:(.text+0x9): undefined reference to `uv_loop_new'
main.c:(.text+0x28): undefined reference to `uv_run'
collect2: error: ld returned 1 exit status**
Where did I go wrong ?
PS: It doesn't work with #include "uv.h"
You need to link the libuv.a with your compiled code and the linker doesn't know where to find the compiled libuv.
To give you a better answer I would need to see you compile command but in the meantime I would strongly recommend this video where Ryan builds a sample libuv project.
The actual code he uses is a little out of date as the API has changed but I think you will find the start where he puts a project together very enlightening.
http://vimeo.com/24713213
In ubuntu I have used following command with success:
gcc sample.c -luv

C++ - undefined reference to `__gxx_personality_seh0'

I am having an issue when trying to build my C++ application in Eclipse (neon.2). This application is simply supposed to create a file, print the numbers 15-210, save the file, then close. This is the error I am getting each time I try to build it:
17:59:42 **** Incremental Build of configuration Debug for project Project1 ****
Info: Internal Builder is used for build
g++ -o Project1.exe Q1.o Q2.o displayHeader.o
Q2.o:Q2.cpp:(.data+0x0): undefined reference to `__gxx_personality_seh0'
Q2.o:Q2.cpp:(.xdata+0x10): undefined reference to `__gxx_personality_seh0'
collect2.exe: error: ld returned 1 exit status
17:59:44 Build Finished (took 1s.391ms)
This only happens when I attempt to use either ofstream or ifstream in my program. I have successfully built & ran other (very simple) C++ applications that don't use this in Eclipse, but when I do, I get this error.
Here is my code:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main(){
ofstream outfile;
outfile.open("nums.txt");
for (int i = 15; i < 211; i++) {
outfile << i << " ";
}
outfile.close();
return 0;
}
I created the project with MingW, and I have the /bin folder in my environment path variable, and under Project/Properties/C/C++ Build/Environment all of the variables are set. I've been searching through forums and tutorials like crazy to try and see if I missed something during installation of either Eclipse or MingW, but I don't see any issues.
I hope I provided adequate information but please let me know if anything else could help. I've been pulling my hair out over this for almost a week. And thank you so much for any help.
You didn't do anything wrong or miss anything during installation!
I had this issue before , this occurs in MingW compiler and ofstream objects, you should change this command
x86_64-w64-mingw32-gcc [your code file]
with this :
x86_64-w64-mingw32-g++ [your code file]
In short , change gcc with c++ in Eclipse or command line.
My opinion, use MS Visual Studio for compiling C++ code in Windows, its compiler give you more features and flexibility and you could develop faster.

waf at linking time: "undefined reference" error

So I've been banging my head on this compiler error for the last 2 hours and thought I would post the code here to see if anyone can shed any light on my mistake.
I have stripped out all the irrelevant bits to leave a bare minimal program (shown below) which as far as I can see should compile and run. If I comme but I can't see what is wrongnt out the call to testFunc in main then everything compiles and runs fine. With the call to testFunc in however, I get the following:
$ ./waf -v --run abr-tool
Waf: Entering directory `/home/conor/workspace/msc/AdaptiveIPTV/Software/conor/ns3/ns-3.15/build'
[1665/1822] cxxprogram: build/src/abr-tools/examples/abr-tool.cc.4.o -> build/src/abr-tools/ns3.15-abr-tool-debug
19:04:19 runner ['/usr/bin/g++', '-L/usr/lib', '-lboost_iostreams', '-L/usr/lib', '-lboost_iostreams', '-pthread', 'src/abr-tools/examples/abr-tool.cc.4.o', '-o', '/home/conor/workspace/msc/AdaptiveIPTV/Software/conor/ns3/ns-3.15/build/src/abr-tools/ns3.15-abr-tool-debug', '-Wl,-Bstatic', '-Wl,-Bdynamic', '-Wl,--no-as-needed', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-lns3.15-point-to-point-debug', '-lns3.15-internet-debug', '-lns3.15-mpi-debug', '-lns3.15-bridge-debug', '-lns3.15-network-debug', '-lns3.15-core-debug', '-lrt']
src/abr-tools/examples/abr-tool.cc.4.o: In function `main':
/home/conor/workspace/msc/AdaptiveIPTV/Software/conor/ns3/ns-3.15/build/../src/abr-tools/examples/abr-tool.cc:7: undefined reference to `testFunc()'
collect2: ld returned 1 exit status
As you can see the code below is being built as part of a much larger project, and I am aware that the error may be coming from that build process rather than an issue with my code, but either way I have hit something of a wall in my understanding of what is happening here. I'm learning c++ as I go and to tell the truth I don't feel experienced enough to even just compile this code on its own and be able to say "that should definitely work but it doesn't", which is why I present it like this.
Another couple of points which might be relevant:
I can use macros defined in abr-helper.h from abr-tools.cc, and the problem persists when I put abr-tools.cc in the same folder as abr-helper.h and just use '#include "abr-helper.h"'.
the original error was the same thing, but for a bunch of other stuff defined in abr-helper.h and use in abr-tools.cc
I would appreciate any help you folks can offer, thanks in advance.
abr-helper.h:
#ifndef ABR_HELPER_H
#define ABR_HELPER_H
#include <iostream>
void testFunc();
#endif /* ABR_HELPER_H */
abr-helper.cc:
#include <iostream>
#include "abr-helper.h"
void testFunc(){
std::cout << "this is all testFunc() does ..." << std::endl;
}
abr-tool.cc:
#include <iostream>
#include "ns3/abr-helper.h"
int main (int argc, char *argv[]){
std::cout << "in main()" << std::endl;
testFunc();
return 0;
}
It appears the file abr-helper.cc doesn't get compiled. You can easily test this by adding a
#error "Test"
line to that file. If the build succeeds, the file isn't getting compiled and you need to add it. How you do that depends on your compiler or IDE.
You need to include abr-helper.cc in your compilation, otherwise the implementation is not linked in.
I don't know waf, so I won't be able to give a solution. But here is what happens :
During compilation, everything works fine. The problem occurs during the linking (ld error).
On the line starting with : 19:04:19 runner, there is no reference to abr-helper.o or something like that. You must have forgotten to add abr-helper.cc somewhere in your waf configuration.
Additionally to the other answers (I also don't see the file abr-tool.cc to be compiled), the function void testFunc needs to be declared extern.

g++ crashes after consecutive calls

I'm developing a tool that makes several calls to g++ in order to compile and execute different versions of an original C++ program. More or less, this calls are made inside a loop (it's not a real "loop", but the calls are made inside a function that is part of an iterative algorithm).
For short executions of the algorithm, everything goes smoothly and all the g++ calls finish correctly (I get the binaries ready for its execution). However, the longer the algorithm executions are, the more likely the tool is to crash.
This crashes are due to subsequents crashes of g++ calls, that instead of returning the expected compiled binaries, return the following errors:
Compiling test_oclopts_23_14_6_6000.cpp...
g++ -O3 -L/opt/AMDAPP/lib -lOpenCL -I/opt/AMDAPP/include test_oclopts_23_14_6_6000.cpp -o test_oclopts_23_14_6_6000
/usr/bin/ld: cannot find /usr/lib/i386-linux-gnu/libc_nonshared.a
/usr/bin/ld: cannot find /lib/i386-linux-gnu/ld-linux.so.2
collect2: ld returned 1 exit status
I've googled a bit about this error messages, but I don't understand how they can be applied to my problem: the supposedly missing libraries really are well installed in the system (former g++ calls went OK).
Any idea? Moreover, if you know a better way to compile C++ source code inside a C++ program instead of using system(), I'm accepting suggestions, :)
Thank you so much in advance,
Jorge.