简体   繁体   English

Linux中的Makefile C

[英]Makefile C in linux

I have 3 functions separated in .c files and the main.c I would like to make the make file, I wrote in the file: 我在.c文件和main.c中有3个函数,我想制作make文件,我在文件中写道:

# Indicate that the compiler is the gcc compiler

CC=gc

# Indicate to the compiler to include header files in the local folder
CPPFLAGS = -I

main: method1.o
main: method2.o
main: method3.o
main: method4.o
main.o: main.h

Whereas method 1,2,3,4 is the functions of the main .c and I have the following problem when I type make in the shell: 而方法1,2,3,4是主要.c的功能,在shell中键入make时会遇到以下问题:

make
gcc  -I  -c -o method1.o method1.c
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [method1.o] Error 1

if your project contains the following files: method1.c method2.c method3.c method4.c and main.c 如果您的项目包含以下文件: method1.c method2.c method3.c method4.cmain.c

you can use the following make file 您可以使用以下make文件

CPPFLAGS=-I/path/to/header/files
CC=gcc
all: main

%.o: %.c
    $(CC) $(CPPFLAGS)  -c -o $@ $^

main: method1.o method2.o method3.o method4.o main.o
    $(CC) -o $@ $^

The issue is in your CPPFLAGS definition: 问题出在您的CPPFLAGS定义中:

# Indicate to the compiler to include header files in the local folder
CPPFLAGS = -I

According to the comment above it, it misses a . 根据上面的评论,它缺少. :

CPPFLAGS = -I.

Otherwise, gcc will treat the -c that comes after -I in your command line as the name of a directory where it can search for headers. 否则, gcc会将命令行中-I后的-c视为可以在其中搜索标头的目录的名称。 Thus, as far as gcc is concerned there's no -c option , and it will attempt to link method1.c as a complete application, hence the error message complaining that there's no main function. 因此,就gcc而言,没有-c 选项 ,它将尝试将method1.c链接为一个完整的应用程序,因此错误消息抱怨没有main功能。

CC=gcc
CPPFLAGS=-I include
VPATH=src include
main: main.o method1.o method2.o method3.o method4.o -lm
    $(CC) $^ -o $@
main.o: main.c main.h
    $(CC) $(CPPFLAGS) -c $<
method1.o: method1.c
    $(CC) $(CPPFLAGS) -c $<
method2.o: method2.c
    $(CC) $(CPPFLAGS) -c $<
method3.o: method3.c
    $(CC) $(CPPFLAGS) -c $<
method4.o: method4.c
    $(CC) $(CPPFLAGS) -c $<

it worked like this 它像这样工作

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

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