Strange output when compiling c++ code. Any Ideas? - c++

When i compile my code i get a set of errors that appear to related to the output files as in the .o file. I'm not sure why these sorts of errors would occur. Any Ideas?
/tmp/ccjPLJVV.o: In function `PubSub::~PubSub()':
Video_process.cpp:(.text._ZN6PubSubD2Ev[_ZN6PubSubD5Ev]+0x12): undefined reference to `vtable for PubSub'
/tmp/ccjPLJVV.o: In function `main':
Video_process.cpp:(.text.startup+0x34): undefined reference to `vtable for PubSub'
Video_process.cpp:(.text.startup+0xeb): undefined reference to `PubSub::run()'
/tmp/ccjPLJVV.o:(.rodata._ZTI13Video_process[typeinfo for Video_process]+0x10): undefined reference to `typeinfo for PubSub'
collect2: ld returned 1 exit status
This is essentially the output i'm getting when I attempt to compile.

It appears you have unimplemented virtual methods.
class PubSub
{
//virtual destructors, although pure
//MUST have an implementation
virtual ~PubSub() = 0 { }
/*virtual?*/ void Run(); // <--- have you implemented this one?
};

Maybe you've implemented the method, but you have not linked it. If you're using GCC, -o flag is your friend; all your class .o files must be specified when compiling the main.cpp.

this is an error message from the linker, not the compiler. The linker cannot find some symbols which are declared, but not defined, in some files it tries to link together to make (most likely) an executable. The solution is to provide the definitions, i.e. the (compiled) code with those definitions. That code may already exist and you just have to "link against it" (tell the linker to search for symbols there) or may not, in which case you have to provide it...
for example, add the file defining the implementations of class PubSub to the linker/compiler command line should help ...

Related

How can I use the library that I have built without error from source, but not compiling for my own project?

I'd like to try the AsmJit library. Building it with 'cmake' and 'make' from source was no problem, the examples it provided were all compiled and executed perfectly. I also did make install to export the dependency files.
I then wanted to compile my own program using this library, so I retrieved the generated files (the headers and the static lib) to add them to a new project whose the code is a copy/paste of the first example given on the library's website:
// #define ASMJIT_NO_DEPRECATED // this line is no part of the original code
#include <asmjit/asmjit.h>
#include <stdio.h>
using namespace asmjit;
// Signature of the generated function.
typedef int (*Func)(void);
int main(int argc, char* argv[]) {
JitRuntime rt; // Runtime designed for JIT code execution.
CodeHolder code; // Holds code and relocation information.
code.init(rt.environment()); // Initialize CodeHolder to match JIT environment.
x86::Assembler a(&code); // Create and attach x86::Assembler to `code`.
a.mov(x86::eax, 1); // Move one to 'eax' register.
a.ret(); // Return from function.
// ----> x86::Assembler is no longer needed from here and can be destroyed <----
Func fn;
Error err = rt.add(&fn, &code); // Add the generated code to the runtime.
if (err) return 1; // Handle a possible error returned by AsmJit.
// ----> CodeHolder is no longer needed from here and can be destroyed <----
int result = fn(); // Execute the generated code.
printf("%d\n", result); // Print the resulting "1".
// All classes use RAII, all resources will be released before `main()` returns,
// the generated function can be, however, released explicitly if you intend to
// reuse or keep the runtime alive, which you should in a production-ready code.
rt.release(fn);
return 0;
}
This is what the hierarchy of my test project looks like:
include\
asmjit\ // The generated headers from the library build
libasmjit.a // The generated static library from the library build
main.cpp // My program's code, as pasted above
And here is the command line used to compile:
g++ main.cpp -o main -Iinclude -L -lasmjit
A first compilation error occurs:
In file included from include/asmjit/./core.h:2008,
from include/asmjit/asmjit.h:27,
from main.cpp:1:
include/asmjit/././core/builder.h:375:20: error: function 'asmjit::Error asmjit::BaseBuilder::dump(asmjit::String&, uint32_t) const' definition is marked dllimport
375 | ASMJIT_API Error dump(String& sb, uint32_t formatFlags = 0) const noexcept {
| ^~~~
After some research in the lib code, defining the ASMJIT_NO_DEPRECATED macro (see in the code above, the first commented line) prevents this error from being reproduced. I doubt this is a good idea, as it may be the cause of the following next link errors:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x35): undefined reference to `__imp__ZN6asmjit10JitRuntimeC1EPKNS_12JitAllocator12CreateParamsE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x45): undefined reference to `__imp__ZN6asmjit10CodeHolderC1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x6e): undefined reference to `__imp__ZN6asmjit10CodeHolder4initERKNS_11EnvironmentEy'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x82): undefined reference to `__imp__ZN6asmjit3x869AssemblerC1EPNS_10CodeHolderE'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x11f): undefined reference to `__imp__ZN6asmjit3x869AssemblerD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x12f): undefined reference to `__imp__ZN6asmjit10CodeHolderD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x142): undefined reference to `__imp__ZN6asmjit10JitRuntimeD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x159): undefined reference to `__imp__ZN6asmjit3x869AssemblerD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x169): undefined reference to `__imp__ZN6asmjit10CodeHolderD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text+0x17c): undefined reference to `__imp__ZN6asmjit10JitRuntimeD1Ev'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text$_ZN6asmjit11BaseEmitter4emitIJRKNS_3x862GpEiEEEjjDpOT_[_ZN6asmjit11BaseEmitter4emitIJRKNS_3x862GpEiEEEjjDpOT_]+0x4c): undefined reference to `__imp__ZN6asmjit11BaseEmitter6_emitIEjRKNS_8Operand_ES3_'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\<my user name>\AppData\Local\Temp\cc4WaQ7J.o:main.cpp:(.text$_ZN6asmjit11BaseEmitter4emitIJEEEjjDpOT_[_ZN6asmjit11BaseEmitter4emitIJEEEjjDpOT_]+0x1b): undefined reference to `__imp__ZN6asmjit11BaseEmitter6_emitIEj'
collect2.exe: error: ld returned 1 exit status
I couldn't find a way to prevent the first error from occurring, except by defining this macro, and I also don't understand why the link editor can't find the references. I have also tried placing these dependencies (headers + static lib) in the appropriate MinGW directories (i.e. the global include and lib directories), but this does not change anything.
How do I compile this program, which is quite simple? I would also like to know why what I did didn't work, knowing the cause of this mistake, I will probably be able to deal with others of the same style in the future.
PS: I don't have much experience with managing external dependencies in C and C++. I am under Windows, using MinGW and the last version of GCC.
You are missing ASMJIT_STATIC compile-time definition - it has to be defined if you use AsmJit statically. This definition is checked at compile time by AsmJit to setup ASMJIT_API macro, which expands to a compiler-specific import/export/visibility attribute.
AsmJit documentation (Build Instructions section) says [1]:
Projects that use AsmJit statically must define ASMJIT_STATIC in all compilation units that use AsmJit, otherwise AsmJit would use dynamic library imports in ASMJIT_API decorator. The recommendation is to define this macro across the whole project that uses AsmJit this way.
So in your particular case this should fix the problem:
g++ main.cpp -o main -Iinclude -L. -lasmjit -DASMJIT_STATIC
NOTE: The documentation link purposely links to the index page so it won't be a dead link once the documentation is reorganized.
[1] https://asmjit.com/doc/index.html

Library using libclang: linker reporting undefined reference to method

as a project of my own, I'm writing a refactoring library (so it can by used by other applications) using libclang for code analysis.
The problem is when i try to compile my program to a static library everything is okey, but when i try to link my library (using some clang code) with some demo application (just main function calling my code) the linker is reporting:
./librefactor.a(renamer.o):(.data.rel.ro+0x60): undefined reference to 'clang::ASTConsumer::HandleInterestingDecl(clang::DeclGroupRef)'
./librefactor.a(renamer.o):(.data.rel.ro+0x80): undefined reference to `clang::ASTConsumer::HandleTopLevelDeclInObjCContainer(clang::DeclGroupRef)'
./librefactor.a(renamer.o):(.data.rel.ro+0x88): undefined reference to `clang::ASTConsumer::HandleImplicitImportDecl(clang::ImportDecl*)'
collect2: error: ld returned 1 exit status
Here is a makefile I build my library with: library makefile
Here is a makefile I build an example with: library example
I commented out some parts of the code to make the error as short as possible but they are some similiar errors (undefined reference) from other part of clang i use in my project.
Problem is I don't understand why linker can't find definitions of these functions/methods. They are not pure virtual in base class so I don't need to defined them. As I see it the linker is trying to find the definition of these in my part of the code istead in clang binaries. Could somebody please explain me where I'm wrong here and explain/repair it?

Mingw and libraw results in undefined reference

Is anyone using libraw with mingw? I am trying to compile a program and get the following errors:
obj\Release\inc\img\cimg.o:cimg.cpp|| undefined reference to `_imp___ZN6LibRawC1Ej'|
obj\Release\inc\img\cimg.o:cimg.cpp|| undefined reference to `_imp___ZN6LibRaw9open_fileEPKcx'|
obj\Release\inc\img\cimg.o:cimg.cpp|| undefined reference to `_imp___ZN6LibRaw6unpackEv'|
obj\Release\inc\img\cimg.o:cimg.cpp|| undefined reference to `_imp___ZN6LibRaw13dcraw_processEv'|
obj\Release\inc\img\cimg.o:cimg.cpp|| undefined reference to `_imp___ZN6LibRaw20dcraw_make_mem_imageEPi'|
obj\Release\inc\img\cimg.o:cimg.cpp|| undefined reference to `_imp___ZN6LibRaw7recycleEv'|
obj\Release\inc\img\cimg.o:cimg.cpp:(.text$_ZN6LibRawD1Ev[LibRaw::~LibRaw()]+0xf)||undefined reference to `_imp___ZN6LibRaw7recycleEv'|
||=== Build finished: 7 errors, 0 warnings ===|
I added libraw.a to my project. Can't figure the problem out.
Has anyone an idea?
From those symbol names, it looks like you're trying to compile against a DLL version of libraw (hinted at by the _imp___ prefix). Check your libraw header files and make sure that the function names don't have anything like __declspec(dllimport) or __attribute__((dllimport)) in front of them. These are sometimes hidden by a macro definition, so check the header files to see if there's another macro you can define which will cause it not to use that attribute in the function prototypes.

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?

Undefined reference linking error when using &MyClass::MyFunction

this just has me stumped, so I thought I'd query here:
I have a class as follows:
class MyClass {
public:
void myThreadFunc();
};
That's in the header. In the constructor
MyClass::MyClass() {
...
boost::thread t(boost::bind(&MyClass::myThreadFunc, this));
...
}
As I've seen done. There are NO compile time errors. However, when I link as follows:
g++ -o test.exe main.o MyClass.o /*specify boost and other libraries */
I get:
MyClass.o:MyClass.cpp:(.text+0xa4): undefined reference to `MyClass::myThreadFunc()'
collect2: ld returned 1 exit status
Which doesn't make any sense. What strikes me especially odd is that's its a linker error. I included both of my object files.
Can anyone tell me what's going on? If it might be relevant, I'm on MinGW on Windows.
EDIT:
Epic fail. I forgot the MyClass:: prefix when defining the function in my cpp file. I just didn't decide to check that. Almost as bad as forgetting a semicolin after a class definition.
You need to write a function body for MyClass::myThreadFunc() somewhere. Writing a constructor for MyClass is different from implementing the MyClass::myThreadFunc() member function.
If you call a function in C/C++, it must have a function body somewhere. That's why it's a linker error; it's trying to find the function body in all of the available object files, but you didn't write one so it can't.