简体   繁体   English

C Makefile 链接错误:未定义对“主”的引用

[英]C Makefile Linking Error: Undefined reference to 'main'

I have tried looking through other answers, unfortunately I have not found anyone with quite the same problem.我尝试过查看其他答案,不幸的是我没有找到任何有相同问题的人。 I am trying to link two C files together, one has a header file, and the other the main file.我正在尝试将两个 C 文件链接在一起,一个有一个 header 文件,另一个是主文件。 When trying to make I receive this error:尝试使我收到此错误时:

(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

Below is the makefile I use to compile.下面是我用来编译的makefile。

INC_DIR = include
SRC_DIR = src
BIN_DIR = bin
UNAME := $(shell uname)
CC = gcc
CFLAGS = -Wall -std=c11 -g -I$(INC_DIR) -I/usr/include/libxml2/
LDFLAGS= -L.

all: XMLParser

XMLParser: $(BIN_DIR)/XMLParser.o $(BIN_DIR)/SVGParser.o
    $(CC) $(CFLAGS) -o XMLParser $(BIN_DIR)/XMLParser.o $(BIN_DIR)/SVGParser.o

$(BIN_DIR)/XMLParser.o: $(SRC_DIR)/XMLParser.c $(INC_DIR)/SVGParser.h
    $(CC) $(CFLAGS) -o $(BIN_DIR)/XMLParser.o -c $(SRC_DIR)/XMLParser.c

$(BIN_DIR)/SVGParser.o: $(SRC_DIR)/SVGParser.c $(INC_DIR)/SVGParser.h
    $(CC) $(CFLAGS) `xml2-config --cflags --libs` -o $(BIN_DIR)/SVGParser.o $(SRC_DIR)/SVGParser.c

clean:
    rm *.o XMLParser $(BIN_DIR)/*

XMLParser.c has my main function: XMLParser.c 有我的主要 function:

int main(int argc, char **argv) {
  ...;
  return 0;
}

Your recipe for generating the intermediate SVGParser.o is attempting to link and generate a runnable executable.您生成中间SVGParser.o的方法是尝试链接并生成可运行的可执行文件。 You need to add -c to skip the link step.您需要添加-c以跳过链接步骤。

You will also need to remove the —libs on that object file and move it to the final link step.您还需要删除—libs文件上的 --libs 并将其移至最后的链接步骤。 In other words add xml2-config —libs for the rule generating the final XMLParser target.换句话说,为生成最终XMLParser目标的规则添加xml2-config —libs

Disclaimer: I've never specifically used xml2-config , so I guess there's some chance I'm wrong here, but it's a very common paradigm for building and linking against complex libraries, I assume it's following the same patterns.免责声明:我从未专门使用过xml2-config ,所以我想我在这里可能错了,但它是构建和链接复杂库的非常常见的范例,我认为它遵循相同的模式。

A full sample code showing the error must be posted to be able to test it elsewhere, so I cannot fully test your makefile, but providing a true main() and see what happens.必须发布显示错误的完整示例代码才能在其他地方进行测试,因此我无法完全测试您的 makefile,但提供一个真正的main()并看看会发生什么。

After generating stub files to check your Makefile I found the following output from make :在生成存根文件以检查您的Makefile ,我从make中找到了以下 output :

$ make
gcc -Wall -std=c11 -g -Iinclude -I/usr/include/libxml2/ -o bin/XMLParser.o -c src/XMLParser.c
gcc -Wall -std=c11 -g -Iinclude -I/usr/include/libxml2/ `xml2-config --cflags --libs` -o bin/SVGParser.o src/SVGParser.c
/usr/local/bin/ld: /usr/lib/gcc/x86_64-unknown-freebsd13.0/10.2.1/../../../crt1.o: en la función `_start':
/home/usr/src/lib/csu/amd64/crt1_c.c:75: referencia a `main' sin definir
collect2: error: ld devolvió el estado de salida 1
*** Error code 1

Stop.
make: stopped in /home/lcu/so/makefile
$ _

which is trying to link (alone) the module SVGParser.o (THIS IS BECAUSE YOU DIDN'T ADD THE -c COMPILATION OPTION TO COMPILE ONLY).它试图(单独)链接模块SVGParser.o (这是因为您没有添加-c编译选项以仅编译)。

Change the compilation line for the SVGParser.o module to use the -c compile-only option and you will get the executable properly.更改SVGParser.o模块的编译行以使用-c compile-only 选项,您将正确获得可执行文件。

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

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