Linker Error Lua 5.1 - c++

I'm compiling something basic with the Lua library, and keep getting a Undefined Reference Error for certain methods.
Not all methods have problems, only loadfile and set/getglobal. I tried replacing set/getglobal with the expanded Macro, but the I get a "LUA_GLOBALSINDEX' was not declared in this scope". Commenting out the offending methods however does allow the file to compile.
The command I use to compile is:
g++ /sourcefiles/ -Wall -lncurses -llua5.1 -lm -ldl

Don't you think that LUA_GLOBALSINDEX is a #define? Defines are not linked from lib files.

Related

Error when linking shared library after compiling objects

I compiled the object files, and then tried to compile the executable, which failed by saying that there are undefined references to functions in "theLib".
g++ -I./theLib/src -c -o obj/main.o src/main.cpp
(works so far)
g++ -L./theLib -Wl,-rpath=./theLib obj/main.o -ltheLib -o exe
(error: libtheLib.so: undefined reference to 'some_function')
I checked for answers everywhere, and they all just suggest moving the -ltheLib part after the dependencies (which I did). But it still doesn't work. What really boggles my mind is that the same library compiles just fine for an example in a different directory.
The library is in C. Can that mess up trying to compile C++? Or am I just missing something with compiling the .o files first?
Turns out the library depended on functions that I had to implement and provide in my own source code. I never knew that some libraries did that, but lesson learned. Once I implemented the functions that were causing the errors and added those source files, it worked.

G++ error from previous declaration, error due to duplicate function name in includes

I am writing a Rcpp code that include two library RcppArmadillo and trng4. However, when I include two header files (RcppArmadillo.h and trng/gamma_dist.hpp) it gives compilation error.
trng/special_functions.hpp:47:39: error: declaration of ‘float lgammaf(float) throw ()’ has a different exception specifier
extern "C" float lgammaf(float) throw();
include-fixed/math.h:476:14: error: from previous declaration ‘float lgammaf(float)’
extern float lgammaf(float);
Full compilation options are
-fopenmp -lgomp -DUSE_R -DNDEBUG -DDISABLE_SINGLE -DNTHROW -DDISABLE_FIO -I/usr/local/include -I"/Users/avi/Library/R/3.0/library/Rcpp/include" -I"/Users/avi/Library/R/3.0/library/RcppArmadillo/include" -fPIC -pipe -std=c++0x -Wall -pedantic -c
Seems like the lgammaf is declared in both header files. I tried using -E with g++ option but that give ld warning " .o, file was built for unsupported file format" and give error function in not available in .Call when I try it to after loading in R. What am I doing wrong?
Perhaps out of context I am using trng4 package to develop a thread gibbs sampler (in openmp) that sample from gamma distribution. I am currently running this MacOS. But eventual it will run in linux server.
It sounds like you do have a problem between Armadillo and trng4. Maybe you should try to, if possible, separate your interface so that you do not need to include from both in the same file.
Someone can correct me if I'm wrong, but I believe you prevent this issue by using #ifndef in each header file so that it's not defined a second time even if it's included a second time. But I guess these aren't your files are they...?
#ifndef __your_unique_header_name__
blah blah
#endif

Undefined Reference Error When Linking to Static Library

I am trying to compile a project that depends on the Xerces XML Parser. The project compiles for Windows without any difficulty, but I'm having some trouble compiling it with g++ in Cygwin.
In order to use Xerces, I am trying to compile my code against the static library libxerces-c.a. But when I do so, I get errors that look like this:
/tmp/cc2QGvMh.o:test.cpp:(.text+0x3a): undefined reference to `xercesc_2_8::DOMImplementationRegistry::getDOMImplementation(unsigned short const*)'
I've inspected the static library using ar, and confirmed that it contains the DOMImplementationRegistry.o file that defines the function that I am calling.
ar -t libxerces-c.a
...
DOMImplementationImpl.o
DOMImplementationRegistry.o
DOMLocatorImpl.o
...
I've also extracted the object files from the library, and used 'nm' to make sure that the function I am calling actually exists:
ar -x libxerces-c.a
nm --demangle DOMImplementationRegistry.o
...
00000080 T xercesc_2_8::getDOMImplSrcVectorMutex()
00000300 T xercesc_2_8::DOMImplementationRegistry::getDOMImplementation(unsigned short const*)
000002a0 T xercesc_2_8::DOMImplementationRegistry::addSource(xercesc_2_8::DOMImplementationSource*)
...
Since I can compile everything for Windows but not with g++, I thought that the error could be in the linker order (similar to the problem described in this question). However, even after changing the linker order, I am still getting the same compiler error. I have tried both
g++ -o test.exe test.cpp -Llib -lxerces-c
and
g++ -o test.exe test.cpp lib/libxerces-c.a
Any ideas?
Your project uses method from xercesc_2_6 namespace as pointed by compiler error message but your library offers xercesc_2_8 version. Problem is probably caused by mismatch between headers you use and library object file.
You didn't say the source of the archive. If it isn't compiled with cygwin, it could be a name mangling problem. Compiling the library from source might well fix this.
It could also be that the archive is built incorrectly so that it has internal resolution problems. Try giving the library name twice.
g++ -o test.exe test.cpp lib/libxerces-c.a lib/libxerces-c.a
If this works, the archive is broken and you should look for or build a new one.
Try the linker option --enable-stdcall-fixup (see 'man ld'). It will care for name mangling and calling conventions:
g++ -o test.exe test.o -Wl,--enable-stdcall-fixup -Llib -lxerces-c

FLTK g++ Compile error

I have an FLTK project that I finished in visual studios and it compiles absolutely fine. But when I use g++ to compile it it gives me this error:
Undefined first referenced
symbol in file
_ZN4ROMS9ROMS_Menu24read_recipes_ingredientsE6String /var/tmp//ccWVvonz.o
_ZN4ROMS9ROMS_Menu12read_catprdsE6String /var/tmp//ccWVvonz.o
_ZN4ROMS9ROMS_Menu11Read_ordersE6String /var/tmp//ccWVvonz.o
ld: fatal: Symbol referencing errors. No output written to a.out
I'm using a shell file with the following instructions to compile my project:
/opt/csw/gcc4/bin/g++ -Wno-deprecated -I/opt/csg/include main.cpp Graph.cpp GUI.cpp
Window.cpp -L/opt/csw/gcc4/lib/libstdc++.a:/opt/csg/lib -lX11
/opt/csw/lib/libjpeg.so.62 /opt/csg/lib/libfltk.a /opt/csg/lib/libfltk_images.a
Again compiles fine in VS but problems in g++. I don't even understand the error, any help is appreciated, thanks. Will post any code if needed.
Undefined symbol means that you compiled by referencing a declaration, but the linker could not find the definition.
I'm not entirely sure what I'm looking at either from the message. Either you are missing the symbols in ROMS or that is where they were referenced.
_ZN4ROMS9ROMS_Menu24read_recipes_ingredientsE6String
Guessing at the demangling...
ROMS::ROMS_Menu::read_recipes_ingredients::String
You may not have included a file?

UPDATE: C++ undefined reference

When I try to run the following C++ program: UPDATE (Updated code since the past link had some errors): http://pastie.org/private/pdpfpzg5fk7iegnohebtq
I get the following:
UPDATE
The errors that arise now are as follows:
Any ideas on that?
Thanks.
You're not linking in GradeBook.o so you're getting an undefefined reference.
Try
g++ GradeBookMain.cc GradeBook.cc -o GradeBookMain
You also have a typo "maximun" instead of "maximum" in GradeBook.h
You didn’t tell your compiler where to find the GradeBook constructor definition (hence “undefined reference”). You need to pass all source files separately to the compiler, or create intermediate object files for all compilation units, and link them together.
Effectively, the easiest solution is this:
g++ GradeBookMain.cc GradeBook.cc -o GradeBookMain
To quote one of my favorite IRC bots: Undefined reference is a linker error. It's not a compile error. #includes don't help. You did not define the thing in the error message, you forgot to link the file that defines it, you forgot to link to the library that defines it, or, if it's a static library, you have the wrong order on the linker command line. Check which one.
C++ is case sensitive. So, for instance you can displayMessage, but what you define is DisplayMessage. Those are two distinct functions. You should change the definition of DisplayMessage to displayMessage, or when you call it call DisplayMessage not displayMessage
What your compiler is telling you, is that the GradeBook class is defined and everything is OK at the compiling stage, but when the time comes to link a complete executable program, it can't find the actual code for that class. And this is because you have compiled and linked only GradeBookMain.cc and not GradeBook.cc. You can compile and link them both at the same time like this:
g++ GradeBookMain.cc GradeBook.cc -o program
Or you can compile them separately and then link together:
g++ -c GradeBookMain.cc -o GradeBookMain.o
g++ -c GradeBook.cc -o GradeBook.o
g++ GradeBookMain.o GradeBook.o -o program
You need to also compile in GradeBook.cc.
At the moment the class itself is not being compiled or linked, and as such, the linker cannot find the GradeBook class - causing it to complain.