简体   繁体   English

Makefile 有两个.c 文件和没有 header

[英]Makefile with two .c files and no header

Trying to write a makefile with two.c files and no header.尝试用两个.c 文件编写 makefile 并且没有 header。 However, it seems examples online have shown the makefile with a header.然而,网上的例子似乎显示了 makefile 和 header。

So i tried to write a header file and it would tell me a function is redefined somewhere else.所以我试图写一个 header 文件,它会告诉我 function 在其他地方被重新定义。 my header consisted of declarations of functions in both my.c files.我的 header 由 my.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

I'm positive that's not how you write a header but ideally I'd like to have my makefile without a header.我很肯定这不是你写 header 的方式,但理想情况下,我希望我的 makefile 没有 header。 Below is my 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

Is it possible to create a makefile without a header?是否可以在没有 header 的情况下创建 makefile? My two.c files are independent of each other.我的两个.c文件是相互独立的。 If so how do I go about doing that?如果是这样,我该怎么做? And if not, what would I put in my header.h to tell the computer that my variables are not being redefined and are independent from each other?如果没有,我会在我的 header.h 中输入什么来告诉计算机我的变量没有被重新定义并且彼此独立?

You can use the following Makefile:您可以使用以下 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>

However, since it seems the two programs are totally independent, I would rather make two separate makefiles.但是,由于这两个程序似乎完全独立,我宁愿制作两个单独的 makefile。

From what I can understand from your question, you don't need a header file in order to compile C code using a Makefile.根据我从您的问题中了解到的情况,您不需要header 文件即可使用 Makefile 编译 C 代码。 Makefiles don't have anything to do with source code. Makefile 与源代码没有任何关系。 All a Makefile does is run a command(s) if the file(s) you've specified have been updated.如果您指定的文件已更新,则 Makefile 所做的只是运行命令。

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

If you run make outfile , this is what happens:如果您运行make outfile ,会发生以下情况:

  1. If outfile does not exist, then make runs command1 , and then runs command2 .如果outfile不存在,则make运行command1 ,然后运行command2
  2. If outfile exists, but infile1 and infile2 have not been updated since then, make will do nothing.如果outfile存在,但infile1infile2起没有更新, make将什么也不做。
  3. If outfile exists and infile1 or infile2 have been updated since outfile was last modified, then make will run command1 and then run command2 .如果outfile存在并且infile1infile2自上次修改outfile以来已更新,则make将运行command1然后运行command2

That's the basics of make and Makefiles.这就是make和 Makefiles 的基础。 The files ( outfile , infile1 , infile2 etc.) and the commands ( command1 , command2 ) can be anything you like.文件( outfileinfile1infile2等)和命令( command1command2 )可以是您喜欢的任何内容 There can also be any number of them present.也可以有任意数量的存在。

When it comes to compiling code, make is pretty useful because you can use it to recompile a binary file (also called an executable file) if the source files are changed, which is exactly what you want.在编译代码时, make非常有用,因为如果源文件发生更改,您可以使用它重新编译二进制文件(也称为可执行文件),这正是您想要的。

For example, this makefile creates a binary file (executable file) from two source files (and no header files):例如,这个 makefile 从两个源文件(没有 header 文件)创建一个二进制文件(可执行文件):

# 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

If you do have header files, then you would need to change the compilation lines to this:如果您确实有 header 文件,那么您需要将编译行更改为:

# 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

If you are not clear about what header files are, then look them up somewhere or ask another question about them.如果您不清楚 header 文件是什么,请在某处查找它们或询问有关它们的另一个问题。

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

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