• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

g++ / gcc compiler - Linking in a compiled exe


I have a program written in c++ and compiled with g++. It runs as a console program, stand alone.

I would like to use one of the classes of this compiled exe in another exe I am making. Both are made in the same environment, ie, same PC, same language (ansi C++), and both compiled in with the same compiler (g++ using MinGW).

Is it possible to compile and link an already compiled exe into a new one. It doesn't really have to be compiled in (ie, I don't really mind if its still a seperate program and I need to include both exe's with the distribution, I just want to be able to call a class in the one exe).

The reason I want to link the compiled class and not an object file is because its use may vary. In some instances it will run stand alone and in others it will be called from the second program.

Hope this makes sense...


2 Solutions
evilrixSenior Software Engineer (Avast)Commented:
>> Is it possible to compile and link an already compiled exe into a new one.
Not really, once an executable has been compiled all the load addresses and offsets are calculated... in a DSO (dynamic shared object) these are not set, rather they are calculated at the point where the dynamic link loader imports the symbols. The best thing to do is to pull the common class(es) out into a separate static library and then link this into both executables.
Imo, it would make more sense to link against the object file.

You'd then have two executables : the first is the one you already have, and is used for "running your class standalone", and the second would perform whatever other functionality it needs (independent of the first executable).
make header and cpp of the class independent of first program. then use cpp and header in both makefiles/projects.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now