How do I interpret and resolve "dyld: Symbol not found ..." [duplicate] - c++

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

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.

Trouble with running code in Visual Studio Code

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.

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.

Undefined reference to `kill'

I developed an application for an ARM7 embedded system in C. Now I want to compile and link it with C++ in order to use some C++ features. To do this, I am using mipsel-elf-g++ instead of mipsel-elf-gcc. I can compile my code with mipsel-elf-g++ successfully, but in linking step I get the errors:
/opt/mipsel/lib/gcc/mipsel-elf/3.4.6/../../../../mipsel-elf/lib/libc.a(lib_a-abort.o): In function```abort':
/cygdrive/d/Files/cross/mips/newlib-1.15.0/newlib/libc/stdlib/abort.c:63: undefined reference to_exit'`
/opt/mipsel/lib/gcc/mipsel-elf/3.4.6/../../../../mipsel-elf/lib/libc.a(lib_a-signalr.o): In function```_kill_r':
/cygdrive/d/Files/cross/mips/newlib-1.15.0/newlib/libc/reent/signalr.c:61: undefined reference tokill'`
collect2: ld returned 1 exit status
I searched about this issue and found that I should implement my own _exit and kill functions, so I added this codes to my project:
void _exit(int code)
{
while(1);
}
int _DEFUN (kill, (pid, sig), int pid _AND int sig)
{
if(pid == __MYPID)
_exit(sig);
return 0;
}
By adding these two functions, the undefined reference to `_exit' error is fixed, but the undefined reference to ``kill' error still exists.
What should I do to fix this issue?
Try wrapping the kill function in extern "C" { … }. And, for clarity, I suggest not using the _DEFUN macro.
I know this is an old question, but I ran against the same problem and found a solution. Add these options to your linker:
-specs=nano.specs -specs=nosys.specs -lnosys
I'm not sure, but the first thing, that I see is, that the parameter "kill" has no type...
But the only undefined reference errors I ever got were linking errors... so are there any libraries you forgot to link to?

can't call any functions with devIL

I would like to be able to use the devIL library for a project I am working on, but for some reason I cannot get it set up correctly. In my project, I have:
#include <IL/il.h>
#include <IL/ilu.h>
#include <IL/ilut.h>
If I do not call any devIL functions, then the file will compile with no errors. However, as soon as I include
ilInit();
I get
undefined reference to `ilInit'
collect2: ld returned 1 exit status
What am I doing wrong?
Edit: I am running Ubuntu 11.10
Apparently you are including the headers, but not linking the binaries. With gcc, this can be done using the -l-option.