How to create separate library for include in C++/Eclipse

Jack BeNimble picture Jack BeNimble · Apr 25, 2009 · Viewed 9.3k times · Source

I've gotten some C++ code to work with the TinyXML parser. However, to do this I had to include the source code from TinyXML with my regular source code. I'd like to have TinyXML included as a separate library. I'm using Eclipse with the Cygwin C++ compiler. What's a good way to do this?

Answer

Trevor Boyd Smith picture Trevor Boyd Smith · Apr 25, 2009

I assume you want to separate the library from your own project's source code... but you don't know how to build when the library is not in the same folder.


Assuming your library has precompiled *.lib and *.h files:

  1. Move the library source code to a separate directory
  2. Menubar "project"
  3. Menu "properties" will open a dialog box for all the project properties there will be a list on the left.
  4. List item "C/C++ Build" will change the GUI and show you all the options for gcc's compiler/linker/assembler ( I never do assembly... so I never do anything with the assembler ). [1]
  5. GCC C Compiler --> Directories:
  6. Green plus icon [2] --> Specify the path of your *.h files
  7. Your compiler should now be happy ( but you will fail linking because the linker doesn't know what the actual definitions of each function are )
  8. GCC C Linker --> Libraries:
  9. Library search path (-L) --> Green plus icon --> Specify the path of your *.lib files
  10. Libraries (-l) --> Green plus icon --> Specify the name of each library you are using
  11. Your linker should now be happy and your code should compile

[Footnote - 1] The GUI C/C++ build pane is a wrapper for gcc's command line compiler/linker... it is just making it easier to use because it shows you everything visually.

[Footnote - 2] The '+' icon is what will tell the compiler where your libraries *.h include files are located. The compiler needs the *.h files to know what function prototypes your library has before it compiles.


Assuming you have the actual ( not compiled ) *.c and *.h:

  1. Do the same steps above except in step 7.
  2. At step 7. you need to make sure the library's *.c files are seen by Eclipse's "managed make". If it doesn't see the source code then you need to specify where the source is so that it will compile it.