【问题标题】:Makefile with two .c files and no header带有两个 .c 文件且没有标题的 Makefile
【发布时间】:2019-09-26 04:54:50
【问题描述】:

尝试编写一个包含两个 .c 文件且没有头文件的 makefile。但是,似乎在线示例显示了带有标题的生成文件。

所以我尝试编写一个头文件,它会告诉我一个函数在其他地方被重新定义。我的头文件由两个 .c 文件中的函数声明组成。

#ifndef HEADER_H 
#define HEADER_H 

void calc(void parameters); 
int main(int argc, char* argv[]); 

struct compArray
   { 
   int start; 
   int end; 
   int thr; 
   int m; 
   int r; 
   };

#endif

我很肯定这不是您编写标题的方式,但理想情况下,我希望我的 makefile 没有标题。下面是我的 Makefile:

all:    thr_atomic.o thr_reduce.o
gcc -o make thr_atomic.o thr_reduce.o -lm -pthread

thr_atomic.o: thr_atomic.c
gcc -c thr_atomic.c

thr_reduce.o: thr_reduce.c
gcc -c thr_reduce.c

是否可以创建没有标头的 makefile?我的两个 .c 文件彼此独立。如果是这样,我该怎么做?如果没有,我会在 header.h 中添加什么来告诉计算机我的变量没有被重新定义并且彼此独立?

【问题讨论】:

  • #ifndef HEADER_H #define HEADER_H void calc(void parameters); int main(int argc, char* argv[]);结构 compArray { int 开始;打算;诠释苏;诠释米;诠释r; }; #endif
  • 我也没有,所以在我的两个 .c 文件中我会在我的函数上使用 extern?
  • void parameters 没有任何意义。也许你的意思是void *parameters?无论如何,没有理由只为生成文件添加标题。除了如何运行它之外,Make 对 C 编译器(或任何其他编译器)的工作一无所知。它当然不知道也不关心您的代码是否使用标头。
  • 在.h文件中,将void calc(...改为extern void calc(...,去掉int main(...这一行。
  • Make 不关心你是否有头文件,如果你没有需要在源文件之间共享的声明,C 不需要它们。你的 C 程序是如何相关的?哪个包含main 函数,如果第一个不调用它的函数,为什么需要另一个?

标签: c makefile


【解决方案1】:

您可以使用以下 Makefile:

all: thr_atomic thr_reduce

thr_atomic: thr_atomic.c 
gcc thr_atomic.c -lm -pthread 

thr_reduce: thr_reduce.c 
gcc thr_reduce.c -lm -pthread <newline>

但是,由于这两个程序似乎完全独立,我宁愿制作两个单独的 makefile。

【讨论】:

    【解决方案2】:

    根据我从您的问题中了解到的情况,您不需要 头文件来使用 Makefile 编译 C 代码。 Makefile 与源代码没有任何关系。如果您指定的文件已更新,Makefile 所做的只是运行一个命令。

    # Makefile
    outfile: infile1 infile2
        command1
        command2
        # ^ commands MUST begin with a tab
    

    如果您运行make outfile,会发生以下情况:

    1. 如果outfile 不存在,则make 运行command1,然后运行command2
    2. 如果outfile 存在,但infile1 并且 infile2 从那时起没有更新,make 将什么都不做。
    3. 如果outfile 存在并且infile1infile2 自上次修改outfile 以来已更新,则make 将运行command1,然后运行command2

    这是make 和 Makefile 的基础。文件(outfileinfile1infile2 等)和命令(command1command2)可以是您喜欢的任何东西。也可以有任意数量的存在。

    在编译代码时,make 非常有用,因为如果源文件发生更改,您可以使用它重新编译二进制文件(也称为可执行文件),这正是您想要的。

    例如,这个 makefile 从两个源文件(并且没有头文件)创建一个二进制文件(可执行文件):

    # Makefile
    
    # executable file is called "prog"
    prog: a.o b.o
        gcc -o prog a.o b.o
        #   ^^^^^^^
        #    tell gcc to name the output file "prog"
    
    # compile the first source file "a.c" to produce the object file "a.o"
    a.o: a.c
        gcc -c a.c
    
    # compile the second source file "b.c" to produce the object file "b.o"
    b.o: b.c
        gcc -c b.c
    

    如果您确实有头文件,那么您需要将编译行更改为:

    # Makefile
    
    # ...
    
    # compile a.c, which has an associated header file a.h 
    a.o: a.c a.h
    #        ^^^ add the header file here
        gcc -c a.c
    

    如果您不清楚头文件是什么,请在某处查找它们或询问有关它们的其他问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多