C++链接器设计问题
因此,这更多的是一个设计问题,尽管有关 g++ 链接器的任何信息都很棒。
我将 .cpp 文件嵌入到以下结构中:
Main
Main/Utilities
Main/Utilities/Data
当我在 Main 中编译主入口点 .cpp 时,我必须指定任何链接的 .cpp 曾经使用过的所有 .cpp 文件 - 这对于我的测试项目来说效果很好,该项目只具有就像 5 个链接文件,但是这个项目会增长得非常快,这显然是不可行的。有办法解决这个问题吗?是什么原因造成的?
我尝试使用“g++ *.cpp”,但它希望我仍然链接其他内容。我必须做类似“g++ main.cpp Utilities/other.cpp Utilities/Data/data.cpp”的事情。 other.cpp 使用 data.cpp,main.cpp 使用 other.cpp。编译 other.cpp 时,我必须像以前一样指定 data.cpp 。
非常感谢您的任何想法或帮助!
So this is more a design question than anything, though any information about the g++ linker would be awesome.
I have .cpp files embedded in the following structure:
Main
Main/Utilities
Main/Utilities/Data
When I compile the main entry point .cpp in Main, I have to specify all the .cpp files any linked .cpp ever uses - this is working fine for my test project which only has like 5 linked files, but this project will grow very quickly and this is obviously infeasible. Is there a way to get around this? What is causing this?
I tried using "g++ *.cpp" but it wants me to link the others still. I have to do something like "g++ main.cpp Utilities/other.cpp Utilities/Data/data.cpp". other.cpp uses data.cpp, and main.cpp uses other.cpp. When compiling other.cpp I have to specify data.cpp like before.
Thank you very very much for any ideas or help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
管理大型项目的一般方法是使用适当的目录结构(就像您已有的目录结构),然后在每个目录中放置一个
Makefile
。顶层Makefile
应递归调用所需的Makefile
。每个Makefile
应指定要从该目录编译的源文件(以及其他编译标志,如果需要)。理想情况下,您应该根据您的要求创建静态库(或者可能是动态库)。一旦编译了所有依赖代码(意味着您有.lib
或.o
文件),那么您应该编译具有入口点的源文件(例如 < code>main 函数(c/c++ 中))并为链接器指定依赖库(ld
)。举个例子,只要看看任何至少有 2 级目录结构的开源项目。
The general approach for managing a big project is to use a proper directory structure (like the one you already have in place) and then place a
Makefile
in each directory. The top levelMakefile
should recursively call the requiredMakefiles
. EachMakefile
should specify the source files that are to be compiled from that directory (and other compilation flags, if required). Ideally you should make static libraries (or maybe dynamic) depending on your requirement. Once you have all the dependent code compiled (means you have.lib
or.o
files with you), then you should compile the source file having the entry point (egmain
function in c/c++) and specifying the dependent libraries to the linker (ld
).For an example, just look at any open source project that has at least 2 level directory structure.
这是错误的。您必须为链接器提供足够的信息来解析代码中使用的所有符号,但您可以将其作为源代码文件(.cpp 等)、目标文件 (.o) 或库(.so 或 .一个)。将每个源代码文件编译为目标文件,然后将每个目标文件链接在一起以创建二进制文件就足够了。
This is false. You must give the linker enough information to resolve all the symbols used in the code, but you can do so as source code files (.cpp et al.), as object files (.o), or as libraries (.so or .a). It is sufficient to compile each of the source code files to object files, and then link each of the object files together in order to create the binary.
我假设您实际上使用的是 UNIX(类似)系统,因此 shell 会将 *.cpp 通配符规范替换为实际文件名。其他系统的行为可能有所不同,尽管我对此表示怀疑。
您已将文件存储在目录结构中,因此简单地说
*.cpp
将仅匹配当前目录中的文件,您在其中调用 g++ - 在您的情况下可能只是 main.cpp。如果您还想用通配符覆盖所有其他文件,则还必须在命令行上指定它们。就您而言:
如果这实际上比简单地命名所有文件更容易,我无法判断。
编辑:正如其他人所说,编译/构建重要程序的最佳方法当然是使用
make
或一些类似的构建工具。I assume that you are actually on a UNIX (like) system, so the shell will replace the
*.cpp
wildcard specification into actual filenames. Other systems may behave differently, though I doubt it.You have stored your files in a directory structure, so simply saying
*.cpp
will only match the file in the current directory, where you invoke g++ - in your case presumably just main.cpp.If you want to cover all the other files with wildcards as well, you'd have to specify them on the command line as well. In your case:
If that is actually easier than simply naming all the files, I can't judge.
EDIT: as others have said, the best way to compile/build non-trivial programs is of course using
make
or some comparable build tool.