Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore the main method from a dynamic linked library in C++

In our application we use several dynamically linked libraries. We recently updated the version of one of these libraries. The developers of that library are providing a main method since this new version (Doing nothing more than printing Hello World).

The problem is that we are using GTest and GMock, which generate their own main method. When we now run our test binary, we just get "Hello World", so the wrong main method is being taken.

How can we force the main method of the tests to be used?

UPDATE: This seems to be a GMock/GTest issue. The main method is normally generated, but this is not the case as it finds an already existing main method now.

like image 361
W. Goeman Avatar asked Mar 14 '12 09:03

W. Goeman


People also ask

Can shared library have main?

No, the implementation of C library of gcc has an entry point not a main symbol.

How does dynamic loader work?

Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory.

How does a shared library work?

Simply put, A shared library/ Dynamic Library is a library that is loaded dynamically at runtime for each application that requires it. Dynamic Linking doesn't require the code to be copied, it is done by just placing name of the library in the binary file.


2 Answers

There are no ways around in your situation. You'll have to remove the main from the shared library. It simply doesn't belong there.

like image 58
BЈовић Avatar answered Oct 23 '22 05:10

BЈовић


I had a similar issue with two libraries having main methods, which was resolved by changing the order of the libraries to the linker.

like image 37
pytflyt Avatar answered Oct 23 '22 03:10

pytflyt