Search code examples
c++eclipsemultiple-projects

Eclipse working with multiple projects


I have two projects written in C++. Suppose that the projects are named first and second, the first being the base project, the second project has addons for the first so when I build and install the second project, it just adds to the functionality of the first.

I created two separate Makefile projects 'first' and 'second' and I am able to build both of them separately.

My problem is that I am not able to link the binaries generated in the 'first' as the base binaries for the 'second'. I tried searching for similar questions on stackoverflow but I did not get any idea on how to link binaries generated in the 'first' to 'second', any help would be greatly appreciated.

Let me know if I need to reframe my question.


Solution

  • If you are using GCC, and read the reference for linker options you will notice to options for linking with external libraries:

    • -L<directory>

      This option tells the linker to add <directory> to the library search path. In other words, it tells the linker where to find library files.

    • -l<name-of-lib>

      This option tells the linker to link with a library. Libraries in POSIX environments (Linux, OSX, Windows using Cygwin or MinGW) are named like libname-of-lib.a, but with the -l option you don't need to use the lib prefix or the .a extension. Also note that in the option -l, that is the small letter L (not capital i or the digit 1).

    To summarise: To link with a library from another directory you link like this:

    $ gcc <other flags> <object files> -o <executable> -L<directory> -l<library>
    

    In your case you should for the -L option specify the directory where the library for project1 is, and for the -l option you pass the basic name of the library.


    If on the other hand you don't turn project1 into a library, and want to link with its object files directly, that's fine too:

    $ gcc <flags> <object files of project1> <object files of project2> -o <executable>
    

    The object files can of course be full or relative paths. For example, if you have the following directory structure:

    /
    `-- home
        `-- user
            `-- myproject
                |-- project1
                `-- project2
    

    Then in if you are in the project2 directory you can access object files from project1 like

    ../project1/objectfile.o
    

    All of this can of course be placed in a Makefile.