简体   繁体   English

如何使用makefile生成.h头文件并在同一个makefile中使用它来编译源代码?

[英]How to generate .h header file using makefile and use it in same makefile for compilation of source?

I want to generate a header file abc.h using makefile.我想使用 makefile 生成一个头文件 abc.h。 And, include this header file in abc.cpp file, which has to be built from same makefile.并且,将此头文件包含在 abc.cpp 文件中,该文件必须从相同的 makefile 构建。

I am facing issue that it is not generating .h file before compiling .cpp file.我面临的问题是在编译 .cpp 文件之前它没有生成 .h 文件。 So, i am getting an fatal error for header file not found.所以,我收到了一个找不到头文件的致命错误。

eg:例如:


//abc.h
#define VAR "My Name"

//abc.cpp
#include "abc.h"

So, question is how to write makefile where it generates header file before compiling source file so that i don't get header file inclusion error?所以,问题是如何在编译源文件之前编写生成头文件的makefile,这样我就不会收到头文件包含错误?

I have created makefile like below but it's not generating file before any other compilation:我已经创建了如下的makefile,但它在任何其他编译之前都没有生成文件:


-include dummy

noinst_LTLIBRARIES = window.la

.PHONY: dummy

dummy:
    @echo "#define VAR "MYNAME" > abc.h

window_la_SOURCES = \
    ../src/abc.cpp 

window_la_CPPFLAGS = \
    -I $(srcdir)/../src/

I want to generate abc.h in above makefile before my compilation begins for abc.cpp.我想在开始为 abc.cpp 编译之前在上面的 makefile 中生成 abc.h。 So, that i don't get fatal error for header inclusion.所以,我不会因为标题包含而遇到致命错误。 Please suggest.请建议。

how to write makefile where it generates header file before compiling source file so that i don't get header file inclusion error?如何在编译源文件之前编写生成头文件的makefile,这样我就不会收到头文件包含错误?

Here is a simple example (assuming that abc.cpp exists, compiles and has main() ):这是一个简单的示例(假设abc.cpp存在、编译并具有main() ):

all: abc

abc.h: Makefile
        @echo "Generating $@"
        @echo "#define VAR \"MYNAME\"" > $@

abc.o: abc.h
abc.cpp: abc.h

abc: abc.o

By default build abc .默认情况下构建abc

The target abc.h is generated by the shell script inside the Makefile and thus depends on the Makefile .目标abc.hMakefile内的 shell 脚本生成,因此依赖于Makefile

Tell make that the abc.cpp requires the abc.h .告诉make abc.cpp需要abc.h

Tell make that abc.o should be rebuild if abc.h changes.如果abc.h发生变化,告诉make应该重建abc.o

Tell make to build the abc from the abc.o .告诉makeabc.o构建abc

Please suggest.请建议。

Code generation in the GNU make -based systems is hard and error prone.基于GNU make的系统中的代码生成很困难并且容易出错。 Though simple example as above seem trivial, on larger projects you might run into various problems for which GNU make offers literally no help in diagnosing or debugging.尽管上面的简单示例看起来微不足道,但在较大的项目中,您可能会遇到各种问题, GNU make在诊断或调试方面实际上没有提供任何帮助。

My suggestion would be, unless you are prepared to read through the whole of GNU make manual , is to avoid that.我的建议是,除非您准备通读GNU make 手册的全部内容,否则请避免这种情况。

Also, for the cases when you just need a preprocessor define, code generation is really an overkill: most IDEs and build systems already provide way to add preprocessor defines to the project.此外,对于只需要预处理器定义的情况,代码生成确实是一种矫枉过正:大多数 IDE 和构建系统已经提供了将预处理器定义添加到项目中的方法。 And many many things in the C/C++ could be reduced to the preprocessor defines. C/C++ 中的许多东西都可以简化为预处理器定义。

Makefiles are a simple way to organize code compilation. Makefile 是一种组织代码编译的简单方法。

You have to create the header file yourself.您必须自己创建头文件。

My answer includes also my particular problem, that I want to have in a .h file that is included by main.c the hash of the commit for which I am compiling.我的回答还包括我的特定问题,我希望在 main.c 包含的 .h 文件中包含我正在编译的提交的哈希。 This is the final version of it, working but only in Git Bash os CMD Line in Windows doesn't know the cut command.这是它的最终版本,但只能在 Windows 的 Git Bash os CMD Line 中工作,不知道 cut 命令。

tagver : ver prebuild postbuild
ver.h: Makefile
    @echo "Generating $@"
    @echo #ifndef INC_VER_H_ > inc\$@
    @echo #define INC_VER_H_ >> inc\$@

    $(eval GitTag_Major := $(shell git rev-parse --short=7 HEAD | cut -b 1))
    @echo "Defining major version to: $(GitTag_Major)"
    @echo #define MAJOR_NUMBER 0x$(GitTag_Major) >> inc\$@

    $(eval GitTag_minor := $(shell git rev-parse --short=7 HEAD | cut -b 2-3))
    @echo "Defining minor version to: $(GitTag_minor)"
    @echo #define MINOR_NUMBER 0x$(GitTag_minor) >> inc\$@

    $(eval GitTag_revision := $(shell git rev-parse --short=7 HEAD | cut -b 4-7))
    @echo "Defining revision version to: $(GitTag_revision)"
    @echo #define REVISION_NUMBER 0x$(GitTag_revision) >> inc\$@

    @echo #endif >> inc\$@
ver: ver.h
main.c: ver.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM