开发者

Using a class from another project in C++

I have access to a large C++ project, full of files and with a very complicated makefile courtesy of automake & friends

Here is an idea of the directory structure.

otherproject/
  folder1/
    some_headers.h
    some_files.cpp
  ...
  folderN/
    more_headers.h
    more_files.cpp

  build/
    lots_of things here
    objs/
      lots_of_stuff.o
      an_executable_I_dont_need.exe

my_stuff/
   my_program.cpp

I want to use a class from the big project, declared in say, "some_header.h"

/* my_program.cpp */
#include "some_header.h"

int main()
{
    ThatClass x;
    x.frobnicate();
}

I managed to compile my file by painstakingly passing lots of "-I" options to gcc s开发者_Python百科o that it could find all the header files

g++ my_program.cpp -c -o myprog.o -I../other/folder1 ... -I../other/folderN

When it comes to compiling I have to manually include all his ".o"s, which is probably overkill

g++ -o my_executable myprog.o ../other/build/objs/*.o 

However, not only do I have to do things like manually removing his "main.o" from the list, but this isn't even enough since I forgot to also link against all the libraries that he happened to use.

otherproject/build/objs/StreamBuffer.h:50: undefined reference to `gzread'

At this point I am starting to feel I am probably doing something very wrong. How should I proceed? What is the usual and what is the best approach this kind of issue?


I need this to work on Linux in case something platform-specific needs to be done.


Generally the project's .o files should come grouped together into a library (on Linux, .a file if it's a static library, or .so if it's a dynamic library), and you link to the library using the -L option to specify the location and the -l option to specify the library name.

For example, if the library file is at /path/to/big_project/libbig_project.a, you would add the options -L /path/to/big_project -l big_project to your gcc command line.

If the project doesn't have a library file that you can link to (e.g. it's not a library but an executable program and you just want some of the code used by the executable program), you might want to try asking the project's author to create such a library file (if he/she is familiar with "automake and friends" it shouldn't be too much trouble for him), or try doing so yourself.

EDIT Another suggestion: you said the project comes with a makefile. Try makeing it with the makefile, and see what its compiler command line looks like. Does it have many includes and individual object files as well?


Treating an application which was not developed as a library as if it was a library isn't likely to work. As an offhand example, omitting the main might wind up cutting out initialization code that the class you want depends upon.

The responsible thing to do here is to read the code, understand it, and turn the functionality you want into a proper library. Build the "exe you don't need" with debug symbols and set breakpoints in the constructors and methods of the class. Step into them so you get a grasp on the functionality and what parts of the program are relevant and irrelevant to your needs.

Hopefully the code is under some kind of version control system that supports branching (such as Git). If not, make your own repository that does. Edit the files until you've organized them into a library and code that uses the library. Make sure it works properly within the context of the original program. Then turn around and use this library in your own program.

If you've done a good job, you might be able to convince the original authors to accept the separation back into their original codebase. If not, at least version control has your back so you can manage integration of future changes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜