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

Posted on 2010-01-07
Last Modified: 2013-11-15

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...

Question by:JSMCM
    LVL 39

    Accepted Solution

    >> 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.
    LVL 53

    Assisted Solution

    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).
    LVL 7

    Expert Comment

    make header and cpp of the class independent of first program. then use cpp and header in both makefiles/projects.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    A list of useful business intelligence software.
    The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
    Video by: Tony
    This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
    XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now