在 C++ 中使用另一个项目中的类;

发布于 2024-11-14 13:44:44 字数 1118 浏览 2 评论 0原文

我可以访问一个大型 C++ 项目,其中充满了文件,并且有一个由 automake & 提供的非常复杂的 makefile。朋友

这是目录结构的一个想法。

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

我想使用大项目中的一个类,在“some_header.h”中声明,

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

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

我通过煞费苦心地将大量“-I”选项传递给 gcc 来编译我的文件,以便它可以找到所有的头文件

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

在编译时我必须手动包含他所有的“.o”,这可能有点过分了

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

但是,我不仅必须做一些事情,比如从列表中手动删除他的“main.o”,而且这还不够,因为我忘了还可以链接到他碰巧使用的所有库。

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

此时我开始觉得我可能做错了什么。 我应该如何进行?此类问题的常见方法和最佳方法是什么?


我需要它在 Linux 上工作,以防需要完成特定于平台的操作。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金兰素衣 2024-11-21 13:44:44

通常,项目的 .o 文件应组合成一个库(在 Linux 上,如果是静态库,则为 .a 文件,或者 .so如果它是动态库),然后使用 -L 选项指定位置并使用 -l 选项指定库名称来链接到该库。

例如,如果库文件位于 /path/to/big_project/libbig_project.a,则需要添加选项 -L /path/to/big_project -l big_project到你的 gcc 命令行。

如果项目没有可以链接到的库文件(例如,它不是库,而是可执行程序,并且您只需要可执行程序使用的一些代码),您可能需要尝试要求项目的作者创建这样的库文件(如果他/她熟悉“automake 和朋友”,那么对他来说应该不会太麻烦),或者尝试自己这样做。

编辑另一个建议:你说该项目带有一个makefile。尝试使用 makefile 来构建它,并查看其编译器命令行是什么样的。它是否也有许多包含文件和单独的目标文件?

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?

〗斷ホ乔殘χμё〖 2024-11-21 13:44:44

将不是作为库开发的应用程序视为库是行不通的。作为一个即兴的例子,省略 main 可能会导致删除您想要的类所依赖的初始化代码。

这里要做的负责任的事情是阅读代码,理解它,并将你想要的功能变成一个合适的库。使用调试符号构建“您不需要的 exe”,并在类的构造函数和方法中设置断点。深入了解它们,以便您了解程序的功能以及程序的哪些部分与您的需求相关和无关。

希望代码处于某种支持分支的版本控制系统(例如 Git)之下。如果没有,请创建自己的存储库。编辑这些文件,直到将它们组织到库和使用该库的代码中。确保它在原始程序的上下文中正常工作。然后转身在你自己的程序中使用这个库。

如果您做得很好,您也许能够说服原始作者接受分离回到他们的原始代码库中。如果没有,至少版本控制可以为您提供支持,以便您可以管理未来更改的集成。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文