如何编写 makefile 来处理头文件中的更改

发布于 2024-12-02 13:17:16 字数 239 浏览 2 评论 0原文

实际上,我有一个库“cryptopp”,我想要的是,当我对文件进行任何更改并发出 make 命令时,它应该处理源目录中任何文件中所做的更改。好吧,cryptoopp 的 GNUMakefile 会处理“.cpp”文件中所做的“if”更改,但不会处理“.h”文件中所做的更改。

那么我可以在 cryptopp 的“GNUMakefile”中进行哪些更改,以便它查看所有修改的头文件并重新编译依赖于“修改的”头文件的所有文件。

Actually i have a library 'cryptopp' and what i want is that when i make any change to a file and issue the make command it should take care of the changes made in any file in the source directory. well, the GNUMakefile of cryptoopp takes care of the changes 'if' made in the '.cpp' files but not for the changes made in a '.h' file.

So what changes can i make in the 'GNUMakefile' of cryptopp so that it looks at all the modified header files and recompiles all the files dependent on the 'modified' header file.

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

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

发布评论

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

评论(3

我不会写诗 2024-12-09 13:17:16

如果您使用 g++ 进行构建,则可以让 g++ 生成依赖 makefile。
您可以将它们包含在主 makefile 中。

使用 -M 和 -M* 参数来使用此功能。 (参见http://gcc.gnu .org/onlinedocs/gcc-4.6.1/gcc/Preprocessor-Options.html#Preprocessor-Options

If you are building with g++ you can let g++ generate dependancy makefiles.
You can include these in your main makefile.

Use the -M and -M* arguments to use this feature. (see http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Preprocessor-Options.html#Preprocessor-Options)

緦唸λ蓇 2024-12-09 13:17:16

您必须将所有依赖项添加到您的 Makefile 中:

mycode.o: mycode.cpp mycode.h somelib.h resources.h
        $(CXX) -c -o $@ 
lt; $(CXXFLAGS) $(INCLUDES)

如果您已经有一个通用模式匹配命令行,则不必再次说出该命令,您只需列出依赖项即可:

%o: %.cpp
        $(CXX) -c -o $@ 
lt; $(CXXFLAGS) $(INCLUDES)

mycode.o: mycode.cpp mycode.h somelib.h resources.h

yourcode.o: yourcode.cpp yourcode.h mycode.h somethingelse.h

# ...

一般来说,这是一个可怕且不可扩展的混乱。您几乎肯定需要一个更高级别的构建系统来为您生成 Makefile。即使对于非常小的项目,在 Makefile 中保持头文件依赖项是最新的也是一件很痛苦的事情,根本不值得。

有几种流行的便携式构建环境。我个人非常喜欢 cmake ,其中包括如果您更改了构建设置(例如从调试到发布)的发现,并且将始终构建所有必要的文件(例如,如果您更改了 cmake 主文件并输入“make”,它会首先自动再次运行 cmake)。

对于仅限 Unix 的解决方案,您可以尝试 makedepend 或臭名昭著的 autotools,尽管这是一个令人头疼的问题......

You have to add all the dependencies to your Makefile:

mycode.o: mycode.cpp mycode.h somelib.h resources.h
        $(CXX) -c -o $@ 
lt; $(CXXFLAGS) $(INCLUDES)

If you already have a generic pattern matching command line, you don't have to say the command again, you can just list the dependencies:

%o: %.cpp
        $(CXX) -c -o $@ 
lt; $(CXXFLAGS) $(INCLUDES)

mycode.o: mycode.cpp mycode.h somelib.h resources.h

yourcode.o: yourcode.cpp yourcode.h mycode.h somethingelse.h

# ...

In general, this is a terrible and unscalable mess. You'll almost definitely want a higher-level build system to generate the Makefile for you. Even for very small projects keeping the header dependencies up to date in the Makefile is such a pain that it is simply not worth it.

There are several popular portable build environments. I personally like cmake a lot, which includes discovery if you changed the build settings (say from Debug to Release) and will always build all the necessary files (for example, if you change the cmake master file and type "make" it'll automatically run cmake again for you first).

For a Unix-only solution you could try makedepend, or the infamous autotools, though that's a whole other headache...

眼眸里的快感 2024-12-09 13:17:16

如果您的系统上安装了“makedepend”,您可以尝试它。最简单的方法是将目标添加到 makefile 中。例如:

depend:
    makedepend *.cc

您可能必须将“*.cc”替换为源文件列表。然后您可以使用“make dependent”命令重新生成所有依赖项。您可能希望将错误消息重定向到 /dev/null,因为它似乎总是会产生大量噪音。

You might try 'makedepend' if it's installed on your system. The easiest way is to add a target to your makefile. Something like:

depend:
    makedepend *.cc

You might have to replace the '*.cc' with a list of your source files. Then you can regenerate all the dependencies with 'make depend' command. You might want to redirect error messages to /dev/null since it always seems to generate a lot of noise.

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