When I've try to compile program (test for my lib) I've got undefined reference of for every called method. I've read answers on "gcc undefined reference to", but it has not help.
PS I using: Debian 7.2.0 and C++11 standart.
#include <RFw/String.hpp>
#include <stdio.h>
using namespace RFw;
int main() {
Array<char> _arr_ (5);
_arr_[0] = 'b';
_arr_[1] = 'c';
printf("%c%c\n", _arr_[0], _arr_[2]);
printf(RFw::getVersion());
return 0;
}
Makefile target:
test:
c++ test.cpp -I./include-core/ -o bin/test -L./bin -l${core_NAME_ROOT}
Console output:
test.cpp:13:9: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
printf(RFw::getVersion());
^~~~~~~~~~~~~~~~~
1 warning generated.
/tmp/test-lxdZF4.o: In function `main':
test.cpp:(.text+0x20): undefined reference to `RFw::Array<char>::Array(int)'
test.cpp:(.text+0x33): undefined reference to `RFw::Array<char>::operator[](int)'
test.cpp:(.text+0x54): undefined reference to `RFw::Array<char>::operator[](int)'
test.cpp:(.text+0x75): undefined reference to `RFw::Array<char>::operator[](int)'
test.cpp:(.text+0x99): undefined reference to `RFw::Array<char>::operator[](int)'
test.cpp:(.text+0xff): undefined reference to `RFw::Array<char>::~Array()'
test.cpp:(.text+0x11a): undefined reference to `RFw::Array<char>::~Array()'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::operator[](int) const'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::Array(int)'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::getLength() const'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Exception::onThrow()'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::resize(int)'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::addElementOnEnd(char)'
./bin/libregemfw0.1-core.so: undefined reference to `vtable for RFw::Object'
./bin/libregemfw0.1-core.so: undefined reference to `typeinfo for RFw::Object'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Object::~Object()'
./bin/libregemfw0.1-core.so: undefined reference to `RFw::Array<char>::~Array()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Ошибка 1
The problem is that
c++ test.cpp -I./include-core/ -o bin/test -L./bin -l${core_NAME_ROOT}c++ test.cpp -I./include-core/ -o bin/test -L./bin -l${core_NAME_ROOT}
will first process the library and then your .cpp file. When processing a library, referenced symbols are resolved ("linked") and all unresolved symbols in the library that aren't needed are thrown away. That means that as soon as your .cpp file is being processed, these symbols are already rejected. You have the library twice in your command line, but the second one is being ignored since the library was already processed.
You should always put the libraries (once) at the end of the compiler command line:
c++ test.cpp -I./include-core/ -o bin/test test.cpp -L./bin -l${core_NAME_ROOT}
Related
I am finding trouble linking a library in C++.
I am trying for the first time to use "matplotlibcpp.h". This is a library that uses "Python.h"
My code is not using either of the libraries yet. It gives error just by including "matplotlibcpp". I am using python2.7 and Ubuntu 18.04 and am using Eclipse.
The code does not run if I include:
#include "matplotlibcpp.h" // programme runs if this is commented out. But I need it to add new features.
#include "Python.h"
I have added these paths in several futile attempts (they solved similar issues to other threads):
GCC C++ Compiler, include paths (-l)
/home/toni/Programs/Workspace/Libraries/matplotlib-cpp-master/
/usr/lib/python2.7/dist-packages/numpy/core/include/numpy
/usr/include/python2.7
GCC C++ Linker, Library search path (-L)
/home/toni/Programs/Workspace/Libraries/matplotlib-cpp-master/
/usr/lib/python2.7/config-i386-linux-gnu
/usr/lib/python2.7
/usr/include/python2.7
The programme builds, but does not run:
Finished building: ../src/WindProfilesCoursera.cpp
Building target: WindProfilesCoursera
Invoking: GCC C++ Linker
g++ -L/home/toni/Programs/Workspace/Libraries/matplotlib-cpp-master/ -L/usr/lib/python2.7/config-i386-linux-gnu -L/usr/lib/python2.7 -L/usr/include/python2.7 -o "WindProfilesCoursera" ./src/WindProfilesCoursera.o
./src/WindProfilesCoursera.o: In function `_import_array':
/usr/include/python2.7/numpy/__multiarray_api.h:1456: undefined reference to `PyImport_ImportModule'
makefile:45: recipe for target 'WindProfilesCoursera' failed
/usr/include/python2.7/numpy/__multiarray_api.h:1460: undefined reference to `PyExc_ImportError'
/usr/include/python2.7/numpy/__multiarray_api.h:1460: undefined reference to `PyErr_SetString'
/usr/include/python2.7/numpy/__multiarray_api.h:1463: undefined reference to `PyObject_GetAttrString'
/usr/include/python2.7/numpy/__multiarray_api.h:1466: undefined reference to `PyExc_AttributeError'
/usr/include/python2.7/numpy/__multiarray_api.h:1466: undefined reference to `PyErr_SetString'
/usr/include/python2.7/numpy/__multiarray_api.h:1478: undefined reference to `PyCObject_Type'
/usr/include/python2.7/numpy/__multiarray_api.h:1479: undefined reference to `PyExc_RuntimeError'
/usr/include/python2.7/numpy/__multiarray_api.h:1479: undefined reference to `PyErr_SetString'
/usr/include/python2.7/numpy/__multiarray_api.h:1483: undefined reference to `PyCObject_AsVoidPtr'
/usr/include/python2.7/numpy/__multiarray_api.h:1487: undefined reference to `PyExc_RuntimeError'
/usr/include/python2.7/numpy/__multiarray_api.h:1487: undefined reference to `PyErr_SetString'
/usr/include/python2.7/numpy/__multiarray_api.h:1493: undefined reference to `PyExc_RuntimeError'
/usr/include/python2.7/numpy/__multiarray_api.h:1493: undefined reference to `PyErr_Format'
/usr/include/python2.7/numpy/__multiarray_api.h:1499: undefined reference to `PyExc_RuntimeError'
/usr/include/python2.7/numpy/__multiarray_api.h:1499: undefined reference to `PyErr_Format'
/usr/include/python2.7/numpy/__multiarray_api.h:1511: undefined reference to `PyExc_RuntimeError'
/usr/include/python2.7/numpy/__multiarray_api.h:1511: undefined reference to `PyErr_Format'
/usr/include/python2.7/numpy/__multiarray_api.h:1522: undefined reference to `PyExc_RuntimeError'
/usr/include/python2.7/numpy/__multiarray_api.h:1522: undefined reference to `PyErr_Format'
collect2: error: ld returned 1 exit status
make: *** [WindProfilesCoursera] Error 1
"make all" terminated with exit code 2. Build might be incomplete.
20:35:04 Build Failed. 22 errors, 1 warnings. (took 2s.454ms)
I am trying to embed python in c++. I am not able to compile some c++ code that uses Python.h. I get an error that seems to be the library is not referenced correctly.
Below is the error shown which I received:
/tmp/ccxiQ8eE.o:(.data+0xc4): undefined reference to Py_NoopCmd(_object*, _object*)'
/tmp/ccxiQ8eE.o:(.data+0xd4): undefined reference toPy_FilterPalletteCmd(_object*, _object*)'
/tmp/ccxiQ8eE.o:(.data+0xe4): undefined reference to Py_FilterCmd(_object*, _object*)'
/tmp/ccxiQ8eE.o:(.data+0xf4): undefined reference toPy_CaptureCmd(_object*, _object*)'
/tmp/ccxiQ8eE.o:(.data+0x104): undefined reference to Py_CaptureBufferCmd(_object*, _object*)'
/tmp/ccxiQ8eE.o:(.data+0x114): undefined reference toPy_StreamCmd(_object*, _object*)'
/tmp/ccxiQ8eE.o:(.data+0x1b4): undefined reference to Py_ModelDebugCmd(_object*, _object*)'
/tmp/ccxiQ8eE.o:(.data+0x1c4): undefined reference toPy_ModelVersionCmd(_object*, _object*)'
/tmp/ccxiQ8eE.o:(.data+0x1d4): undefined reference to Py_ModelSimRunCmd(_object*, _object*)'
/tmp/ccxiQ8eE.o:(.data+0x1e4): undefined reference toPy_ModelSimEndCmd(_object*, _object*)'
collect2: error: ld returned 1 exit status
make: *** [xgs_sim.linux] Error 1
For these functions prototype declaration has been done in some other test.h file and that file has been included while compiling in the program.cc file.
The function definition has been done in test.cc file.
Python version used is python 2.7.1.
Any help/guidelines will be much appreciated. Thanks.
I'm using robo robo simulator. It requires SDL to be installed. SDL was successfully installed using sudo apt-get install in Ubuntu
'make' was used to compile and link.
compilation was successful but while linking I got the following linker errors
[LD] roborobo
src/core/roborobo.o: In function `clean_up()':
roborobo.cpp:(.text+0x29): undefined reference to `SDL_FreeSurface'
roborobo.cpp:(.text+0x35): undefined reference to `SDL_FreeSurface'
roborobo.cpp:(.text+0x41): undefined reference to `SDL_FreeSurface'
roborobo.cpp:(.text+0x4d): undefined reference to `SDL_FreeSurface'
roborobo.cpp:(.text+0x59): undefined reference to `SDL_FreeSurface'
src/core/roborobo.o:roborobo.cpp:(.text+0x6a): more undefined references to `SDL_FreeSurface' follow
src/core/roborobo.o: In function `checkQuitEvent()':
roborobo.cpp:(.text+0xdff): undefined reference to `SDL_PollEvent'
src/core/roborobo.o: In function `handleKeyEvent(unsigned char*)':
roborobo.cpp:(.text+0xead): undefined reference to `SDL_Delay'
roborobo.cpp:(.text+0xf1a): undefined reference to `SDL_Delay'
roborobo.cpp:(.text+0xf43): undefined reference to `SDL_Delay'
roborobo.cpp:(.text+0x1037): undefined reference to `SDL_Delay'
roborobo.cpp:(.text+0x111e): undefined reference to `SDL_Delay'
src/core/roborobo.o:roborobo.cpp:(.text+0x1191): more undefined references to `SDL_Delay' follow
and many more
SDL_gfxPrimitives.c:(.text+0x7477): undefined reference to `SDL_UnlockSurface'
SDL_gfxPrimitives.c:(.text+0x7491): undefined reference to `SDL_UpperBlit'
SDL_gfxPrimitives.c:(.text+0x74e6): undefined reference to `SDL_CreateRGBSurface'
collect2: error: ld returned 1 exit status
make: [roborobo] Error 1 (ignored)
the make file also has sdl-config --cflags --libs -lSDL_image
the Makefile is in http://pastebin.com/5EdcZWAd
the entire console output after 'make' is in http://pastebin.com/yXDHR9xw
I need to call some methods from a C++ program in a C program.
But I just have one shared library (.dll) to use those methods.
To make it, first a create a wrapper in C to call the methods from the dll to be used in the C program. I have attention and I make sure that I declare the functions with extern "C" to avoid name mangling.
But when a compile I have a set of errors (shown in down) when a linking wall the .o's:
g++ -o bonnie++ bonnie++.o bon_io.o bon_file.o bon_time.o semaphore.o sync.o thread.o bon_suid.o duration.o rand.o util.o utils.o super.o pcm_file.o inode.o iname.o dir.o pcm.o -lpthread -L./ -lSmart
pcm.o:pcm.c:(.text+0x4d): undefined reference to `_imp___ZN10Management11OpenEm'
pcm.o:pcm.c:(.text+0x64): undefined reference to `_imp___ZN10Management12CloseEm'
pcm.o:pcm.c:(.text+0x73): undefined reference to `_imp___ZN10Management17EnumerateEv'
pcm.o:pcm.c:(.text+0xb9): undefined reference to `_imp___ZN18DemonstratorDevC1Ev'
pcm.o:pcm.c:(.text+0xf7): undefined reference to `_imp___ZN18DemonstratorDevD1Ev'
pcm.o:pcm.c:(.text+0x121): undefined reference to `_imp___ZN18DemonstratorDevice11OpenEi'
pcm.o:pcm.c:(.text+0x138): undefined reference to `_imp___ZN18DemonstratorDev12CloseAdapterEv'
pcm.o:pcm.c:(.text+0x15f): undefined reference to `_imp___ZN18DemonstratorDev23ConfigureClockFrequencyEi15_ClockFrequency'
pcm.o:pcm.c:(.text+0x176): undefined reference to `_imp___ZN18DemonstratorDev21AdaptationBlockEnableEv'
pcm.o:pcm.c:(.text+0x1a1): undefined reference to `_imp___ZN18CommandsManagementC1ER18DemonstratorDev'
pcm.o:pcm.c:(.text+0x1df): undefined reference to `_imp___ZN18CommandsManagementD1Ev'
pcm.o:pcm.c:(.text+0x21a): undefined reference to `_imp___ZN18CommandsManagement20DeviceInitializationEim9TimerCtrl'
pcm.o:pcm.c:(.text+0x253): undefined reference to `_imp___ZN18CommandsManagement24EnableEmbeddedOperationsER8OWStatusim9TimerCtrl'
pcm.o:pcm.c:(.text+0x290): undefined reference to `_imp___ZN18CommandsManagement12RegionUnlockEmmim9TimerCtrl'
pcm.o:pcm.c:(.text+0x2da): undefined reference to `_imp___ZN18CommandsManagement17BufferedOverwriteEmmPKhiiim9TimerCtrl'
pcm.o:pcm.c:(.text+0x32c): undefined reference to `_imp___ZN18CommandsManagement9BurstReadEmPtmih9BurstTypeim9TimerCtrl'
/usr/lib/gcc/i686-pc-cygwin/4.7.3/../../../../i686-pc-cygwin/bin/ld: pcm.o: bad reloc address 0x20 in section `.eh_frame$_ZN10ManagementC1Ev'
collect2: error: ld returned 1 exit status
Makefile:37: recipe for target `bonnie++' failed
make: *** [bonnie++] Error 1
Someone know what I am doing wrong?
Do you include the extern "C" function declaration header files in your wrapper method source file? if not, this error will occur
Every time I make a new module and try to link it to my main class (Estudiant.o) using this line:
g++ -o red1.exe red1.o %OBJETOS_CPP%\Estudiant.o
I get this error:
red1.o:red1.cpp:(.text+0xd): undefined reference to
Estudiant::consultar_DNI() const' red1.o:red1.cpp:(.text+0x18):
undefined reference toEstudiant::Estudiant(int)'
red1.o:red1.cpp:(.text+0x25): undefined reference to
Estudiant::consultar_nota() const' red1.o:red1.cpp:(.text+0x74):
undefined reference toEstudiant::afegir_nota(double)'
red1.o:red1.cpp:(.text+0x83): undefined reference to
Estudiant::~Estudiant()' red1.o:red1.cpp:(.text+0xa3): undefined
reference toEstudiant::consultar_nota() const'
red1.o:red1.cpp:(.text+0xe5): undefined reference to
Estudiant::modificar_nota(double)' red1.o:red1.cpp:(.text+0x10b):
undefined reference toEstudiant::Estudiant()'
red1.o:red1.cpp:(.text+0x13a): undefined reference to
Estudiant::llegir_estudiant()' red1.o:red1.cpp:(.text+0x144):
undefined reference toEstudiant::te_nota() const'
red1.o:red1.cpp:(.text+0x182): undefined reference to
Estudiant::escriure_estudiant() const' red1.o:red1.cpp:(.text+0x18c):
undefined reference toEstudiant::~Estudiant()'
red1.o:red1.cpp:(.text+0x19f): undefined reference to
Estudiant::~Estudiant()'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe:
red1.o: bad reloc address 0x0 in section.ctors' collect2.exe: error:
ld returned 1 exit status
And I don't have the original Estudiant.cpp so I thought it might be because different compilers were used for compiling and for the link, but I reinstalled MinGW and I'm still getting this error.
I also tried to replace all the files and it didn't work.
I managed to get the source code of the Estudiant.o and compiled another version of it using the code and it worked. I don't know why the same code was compiled on two different machines and one worked and the other one didn't.