Automatically resolve undefined references from missing library with stubs? - c++

I am building some unit testing stuff for a C/(C++) project that was developed for MSVC. I need to port it to g++ / MinGw. There is a library (DLL) that was build with VS. I can not get this library to link with the rest of the code, i always get undefined reference errors. It seems that the name mangeling between MSVC and g++ not compatible. Unfortunately I can not recompile the library with g++ because it uses some Microsoft Stuff that is incompatible with g++ (MFC/AFX).
So I wonder if there is any way to tell g++ to replace all undefined references with simple stub functions that just return 0 or Null.
Because I dont really need any of the functions, and I won't even call functions that call any of the missing functions. I just want to have something there so that the project compiles without changing the code that i want to test.
Is there any way to do that? Is there a tool that maybe extracts all the exposed functions and creates a replacement lib that only contains the stub functions?

Related

Undefined reference error to std::string and std::vector class methods [duplicate]

Are there any differences in the linking process between gcc and g++?
I have a big C project and I just switched part of the code to C++. The code isn't using std C++ library yet, so -llibstdc++ isn't needed for now.
The main difference is that (assuming the files are detected as C++) g++ sets up the flags needed for linking with the C++ standard library. It may also set up exception handling. I wouldn't rely on the fact that just because your application doesn't use the standard library that it isn't needed when compiled as C++ (for example the default exception handler).
EDIT: As pointed out in comments you'll have trouble with any constructors (that do work) for static objects as well as not getting virtual function tables (so if you're using those features of C++ you still need to link that library).
EDIT2: Unless you're using C99 specific code in your C project I would actually just switch to compiling the whole thing as C++ as the first step in your migration process.
gcc and g++ are both just driver programs that don't do anything other than calling other programs, so you can use the -v option to see exactly what they do -- what other programs they invoke with what args. So you can see exactly what the difference is between linking with gcc and g++ for the specific version and architecture of gcc that you happen to have installed. You can't rely on that staying the same if you want portability, however.
Depending on what you are doing, you might also be interested in the -### argument
I think that the g++ linker will look for the CPP mangled function names, and it is different from the C ones. I'm not sure gcc can cope with that. (Provided you can explicitly use the C version rather than the C++ one).
Edit:
It should work if you have
extern "C" {
<declarations of stuff that uses C linkage>
}
in your code and the object file has been compiled with g++ -c. But I won't bet on this.

G++ new ABI problems

I got a problem with the new ABI introduced for C++11 in GCC. After upgrading to GCC 5.3 my project does no longer compile. The error messages I get are simple:
undefined reference to `tokenize(std::__cxx11::basic_string' ...more characters
or
undefined reference to `extract(std::string const&)'
So, it looks like I messed something up and GCC is unable to decide whether I want the old ABI or the new one (the __cxx11:: part is missing from some error messages, and present in others)?
I tried several solutions to resolve the issue:
passing -D_GLIBCXX_USE_CXX11_ABI=0 to GCC,
passing -D_GLIBCXX_USE_CXX11_ABI=1 to GCC,
setting the macro directly in source code,
setting the abi_tag attribute on the declarations GCC complained about when passed the -Wabi-tag flag,
Unfortunately, neither of them worked (i.e. allowed the code to compile). The one thing I know is that only functions returning std::string or taking it as a parameter fail to link. Which is to be expected, given what I read about the problem on the Internet. I was unable to reproduce the issue in a simple, example program to present it here.
Is there any obvious solution to my problem, that I am missing?
This error indicates that you're linking to some code or library that has not been recompiled by gcc 5.3, and was compiled by an earlier version of gcc, using the earlier version of the ABI.
If you are linking with some external libraries, besides the standard C++ library, those external libraries need to be recompiled (and reinstalled).
If you are not linking with any external libraries, and you are only linking together your own code, some of your source modules must not've been recompiled yet. Recompile everything. Make sure to wipe all existing object modules, with make clean, or the equivalent for whatever build system you're using.

How can i compile a project with libqrencode?

I am trying to compile this code but it always gives me "Undefined reference to QRcode_encodeString and QRcode_free". I have done the ./configure, make and make install, no errors where shown. I have no idea what flags i need to use in other to compile it. I'm currently using slackware 3.10 i686. I'm only trying to compile with gcc -Wall main.c. I'm still trying to understand linux libraries and shared objects. Any clue to what might be the problem? My source code is in the same directory as the qrencode.h file. I tried #include and "qrencode.h".
You're forgetting to link with your library.
In other words, the compiler knows what to do with the code (since you're including the header and so on), but the linker does not (since it doesn't "know" where to find the implementation of that --- you need to tell it!).
Add -lqrencode to your compiler flags. If the library is in some directory not searched by default, you also need to add -L/path/to/libdir.
This may be of some help (it gives a few examples for compiling and linking): https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
One additional node: The linker is already involved in building your program: It links your separate object files (assuming you have multiple *.c files) into one. It also it links with at least libc; however, that library is special in that GCC links it implicitly (that is, you don't have to say -lc), since it's the C standard library.

How does C++ linkage works in this case?

I built a C++ library for making games in most known platforms and it works alright. However, I ran into a minor problem with the linker, which I can't live with myself without figuring the answer.
I have two separate classes in two separate files: KinesisWorld in KinesisWorld.h and ASEngine in ASEngine.h . Both of them have a similar behavior in terms of implementation, they encapsulate functionality provided by other libraries, KinesisWorld inherits and calls Box2D functions and ASEngine calls angelscript functions. So far so good.
Now, when I am building an application that uses my library, as long as I don't include or use KinesisWorld, the linker won't attempt to link the Box2D library. As soon as I simply include KinesisWorld.h somewhere in the final executable source, it immediately complains of undefined references until I add Box2D to the linker. This is the behavior I always known and observed with other libraries as well.
Though, with angelscript, without including ASEngine.h at all, it will always complain about undefined references to angelscript, even if the final executable doesn't make any reference to it whatsoever.
Any idea of what can cause such thing? I tested it on Linux 32 bit and Windows 32 bit, with GCC and Visual Studio respectively, same behavior.
The linker doesn't care until the offending (declared but undefined) functions are called.
In this case, you aren't using any Box2D functions anywhere except in KinesisWorld but you ARE using angelscript somewhere in your library. Double check your ASEngine facade but in particular everywhere else (using the "find" feature across your entire project might help).

C++ GNU linker errors

I'm trying to compile my program on Windows via Cygwin with the compilation command:
g++ ping.cpp -I./include -L./lib -lchartdir50
I'm using an API called ChartDirector which draws charts for me. I've never linked libraries this way before (usually I do it through Visual Studio), so I’m a little new to this. I've got a really large list of errors, so I won't list them all, but I’ll list one just to clarify the type of linker errors I’m getting:
(.text$_ZN9BaseChartD1Ev[BaseChart::~BaseChart()]+0x4f): undefined reference to '_CBaseChart_destroy'
All of these are undefined reference to 'xxx' errors.
I've got a bunch of header files in ./include and a library called chartdir50.lib in ./lib.
What’s wrong with my compilation line?
I never use that library before, but when I googled it, I noticed that other people trying like -lchartdir instead of -lchartdir50, so you should give it a try.
I am not sure, but .lib have been compiled with Visual C++?
If yes, I don't think it's compatible with GCC. You have to compile the library with GCC/G++ and use that file or to use a compatible binary if you don't have access to the source.
Hmm... that’s odd. I'm using a 64-bit system, but for some reason I tried it with the 32-bit library and it compiled. Thanks!
I assume that library was also created with GCC.
As far as I know, .lib is a static library, so you don't have to point it with the -l compiler switch.
Just use it as another file on the command line, like
g++ ping.cpp -I./include -L./lib {path to lib here}/chartdir50.lib