I'm not sure to understand the undefined reference I am getting.
./cxxtest/cxxtestgen.py -o tests.cpp --error-printer DrawTestSuite.h
g++ -I./cxxtest/ -c tests.cpp
g++ -o tests tests.o Color.o
tests.o: In function `DrawTestSuite::testLinewidthOne()':
tests.cpp:(.text._ZN13DrawTestSuite16t… undefined reference to `Linewidth::Linewidth(double)'
tests.cpp:(.text._ZN13DrawTestSuite16t… undefined reference to `Linewidth::draw(std::basic_ostream<char… std::char_traits<char> >&)'
collect2: ld returned 1 exit status
make: *** [tests] Error 1// DrawTestSuite.h
DrawTestSuite.h contains the unit-test and The test function calls on Linewidth.h to execute the constructer and member function draw.
I have #include "Linewidth.h"in DrawTestSuite.h.
"Undefined reference" is a linker error when a function has been properly declared and used, but the definition has not been included when linking.
You need to link with Linewidth.o, which is the object file from compiling Linewdith.cpp and the likely location where those functions are implemented.
I'm not familiar with cxxtest to know how it expects you to specify that dependency, but I suspect it only requires a simple declaration.
Related
I'm building a static library for a small project, and when I compile it with ar, it correctly links.
When I go to include the relevant header file and link the test script to the archive;
LINK = -lpthread -lcryptopp -L./path/to/archive/ -luttu
r: ../inc/uttu.hpp
g++ -std=c++20 rnet.cpp -o r.out $(LINK)
I get linker errors;
/usr/bin/ld: /tmp/cc7h6RHu.o: in function `main':
rnet.cpp:(.text+0x263): undefined reference to `np::np()'
/usr/bin/ld: rnet.cpp:(.text+0x317): undefined reference to `np::np()'
/usr/bin/ld: ./../exe//libuttu.a(peer.o): in function `Peer::Connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
/home/uton/code/Concord/uttu/src/peer.cpp:43: undefined reference to `void np::target<np::_tf>(np::_tf)'
/usr/bin/ld: ./../exe//libuttu.a(relay.o): in function `Relay::Foward()':
/home/uton/code/Concord/uttu/src/relay.cpp:40: undefined reference to `np::np()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:8: r] Error 1
Running ar t libuttu.a returns
linux.o
uttu.o
timeout.o
peer.o
relay.o
sec.o
np.o
My test script includes the main header file, uttu.hpp, which itself refers to the np header file, under the name of protocols.hpp
The path to the archive file is correct, the archive itself contains the correct object file, and the test script refers to the main header file, which includes the correct definitions.
I'm stuck on what could be going wrong.
After reading this SO question, I realized that I had missed an overridden virtual member.
I am a beginner to organizing C++ projects and I have been trying to start an Open Dynamics Engine project for a week now but I keep getting several errors. First, what I have done:
Installed ode-0.13.1 with automake as directed in the manual.
Created a file test.cpp with the code from the bouncing ball HOWTO.
Wrote a makefile to compile.
But when I run my makefile, I get the following errors:
gcc -Wall -g -DdDOUBLE -o test test.o -L/home/username/ode-0.13.1/ode/src/libode.la -L/home/username/ode-0.13.1/drawstuff/src/libdrawstuff.la -L/usr/X11R6/lib -L/usr/lib -L/usr/local/lib -I. -I/home/username/ode-0.13.1/include/ -I/usr/X11R6/include -I/usr/include -lm -lode -lGL -lGLU
/usr/local/lib/libode.a(collision_trimesh_box.o): In function dCalcVectorLength3':
/home/username/ode-0.13.1/ode/src/../../include/ode/odemath.h:148: undefined reference tosqrt'
/home/username/ode-0.13.1/ode/src/../../include/ode/odemath.h:148: undefined reference to sqrt'
/home/username/ode-0.13.1/ode/src/../../include/ode/odemath.h:148: undefined reference tosqrt'
/usr/local/lib/libode.a(util.o): In function dxStepBody(dxBody*, double)':
/home/username/ode-0.13.1/ode/src/util.cpp:618: undefined reference tosincos'
/home/username/ode-0.13.1/ode/src/util.cpp:630: undefined reference to sincos'
/home/username/ode-0.13.1/ode/src/util.cpp:628: undefined reference tosqrt'
/home/username/ode-0.13.1/ode/src/util.cpp:590: undefined reference to `sqrt'
... (and this keeps going for pages)
/usr/local/lib/libode.a(OPC_AABBTree.o): In function Opcode::AABBTree::Build(Opcode::AABBTreeBuilder*)':
/home/username/ode-0.13.1/OPCODE/OPC_AABBTree.cpp:413: undefined reference tooperator new[](unsigned long)'
/home/username/ode-0.13.1/OPCODE/OPC_AABBTree.cpp:427: undefined reference to operator new[](unsigned long)'
/usr/local/lib/libode.a(OPC_AABBTree.o): In functionOpcode::AABBTreeNode::~AABBTreeNode()':
/home/username/ode-0.13.1/OPCODE/OPC_AABBTree.cpp:83: undefined reference to operator delete[](void*, unsigned long)'
/usr/local/lib/libode.a(OPC_Collider.o): In functionOpcode::Collider::~Collider()':
/home/username/ode-0.13.1/OPCODE/OPC_Collider.cpp:54: undefined reference to operator delete(void*, unsigned long)'
/usr/local/lib/libode.a(OPC_Collider.o):(.data.rel.ro._ZTIN6Opcode8ColliderE[_ZTIN6Opcode8ColliderE]+0x0): undefined reference tovtable for __cxxabiv1::__class_type_info'
/usr/local/lib/libode.a(OPC_Collider.o):(.data.rel.ro._ZTVN6Opcode8ColliderE[_ZTVN6Opcode8ColliderE]+0x20): undefined reference to `__cxa_pure_virtual'
collect2: error: ld returned 1 exit status
makefile:14: recipe for target 'test' failed
make: *** [test] Error 1
These errors seem like the library has not been set up correctly, but I'm pretty sure I've properly installed and my -L's are set to the correct paths.
Note 1: the demos included with the ode library work.
Note 2: I've tried (and failed) to install other versions of ode previously. I did my best with make uninstall and make clean to clean these failed installs, but I think they may be affecting new installs because when I run sudo make install for the first time there is an awful lot of Nothing to do here... messages.
With the help of Shared libraries with GCC on Linux I tried to make libadd1.so and succeeded. The code runs perfectly.
But when I tried the same thing using dlopen() and dlsym() I got errors as shown below.
$ g++ -L/home/shreya/Desktop/soFiles -Wall -o test main.c -ladd1
main.c: In function ‘int main()’:
main.c:14:14: warning: variable ‘ptrVar’ set but not used [-Wunused-but-set-variable]
pt2function ptrVar;
^
/tmp/ccpdIje6.o: In function `main':
main.c:(.text+0x19): undefined reference to `dlopen'
main.c:(.text+0x29): undefined reference to `dlerror'
main.c:(.text+0x54): undefined reference to `dlerror'
main.c:(.text+0x68): undefined reference to `dlsym'
main.c:(.text+0x71): undefined reference to `dlerror'
main.c:(.text+0x1ca): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
$
Can any one tell me what's wrong?
See the manpage. For Linux, you have to link with -ldl for using the dynamic linking functions. This is a bit of a hassle because e.g. on FreeBSD, they are part of the standard library.
BTW, linking the library that you want to open dynamically (in your case -ladd1) doesn't make sense.
I have seen a lot of similar posts, but tried every trick in the book and am still struggling. Everything was working fine, but after installing/removing wireshark with some components/disselectors it all got messed up. I don't remember exactly which libraries/packages got uninstalled, but probably a lot more than I noticed.
If I create a simple main.cpp file like this one:
#include <SQLAPI.h>
int main()
{
SAConnection con;
return 0;
}
and try
g++ main.cpp -lsqlapi -ldl
it gives me the following error messages:
/usr/local/lib/libsqlapi.so: undefined reference to `dlsym'
/usr/local/lib/libsqlapi.so: undefined reference to `dlerror'
/usr/local/lib/libsqlapi.so: undefined reference to `dlopen'
/usr/local/lib/libsqlapi.so: undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
I have tried to put -ldl before -lsqlapi as some have suggested that the order is important. If I use gcc instead of g++ the error is:
/usr/bin/ld: /tmp/ccwBI4tj.o: undefined reference to symbol '__gxx_personality_v0##CXXABI_1.3'
/usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I am able to compile and run the file if SAConnection is removed.
I don't think it has anything to do with SQLAPI, because I experience similar problems with libboost. I don't have a small code example, but when I compile a project that was successfully compiled last week, I get the error:
/usr/bin/ld: debug/components/helloworld/HelloWorld.o: undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/usr/lib/x86_64-linux-gnu/libboost_system.so.1.53.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
This project is using a Makefile that has been unchanged, so it has to be something on my system that is not correct. I have tried to reinstall build-essential.
Using Ubuntu 64 bit 13.10 with g++ version 4.8.1.
I have found the solution; setting the -Wl,--no-as-needed before -ldl. The new compile command is:
gcc main.cpp -lsqlapi -lstdc++ -Wl,--no-as-needed -ldl
Apparently it has something to do with recent versions of gcc/ld linking with --as-needed by default.
I am trying to use Goblin library which is used for special network algorithms. This library provides some header files and objects in C/C++. So, you can easily add a header file to your program and use some special classes and functions.
Unfortunately, when I add the header file, I get error. In the following you can see the simple code and error.
Source:
#include<goblin.h>
int main()
{
return 0;
}
Error:
$ g++ -o test.o test.cpp
/tmp/ccB0Rb25.o: In function `goblinRootObject::~goblinRootObject()':
test.cpp:(.text._ZN16goblinRootObjectD1Ev[goblinRootObject::~goblinRootObject()]+0x10): undefined reference to `goblinNObjects'
test.cpp:(.text._ZN16goblinRootObjectD1Ev[goblinRootObject::~goblinRootObject()]+0x18): undefined reference to `goblinNObjects'
test.cpp:(.text._ZN16goblinRootObjectD1Ev[goblinRootObject::~goblinRootObject()]+0x2c): undefined reference to `goblinRootObject::operator delete(void*)'
/tmp/ccB0Rb25.o: In function `goblinRootObject::~goblinRootObject()':
test.cpp:(.text._ZN16goblinRootObjectD0Ev[goblinRootObject::~goblinRootObject()]+0x10): undefined reference to `goblinNObjects'
test.cpp:(.text._ZN16goblinRootObjectD0Ev[goblinRootObject::~goblinRootObject()]+0x18): undefined reference to `goblinNObjects'
test.cpp:(.text._ZN16goblinRootObjectD0Ev[goblinRootObject::~goblinRootObject()]+0x2c): undefined reference to `goblinRootObject::operator delete(void*)'
collect2: ld returned 1 exit status
try:
g++ -lgoblin -L/pathToLibgoblin/ -o test.o test.cpp