C++ 在 crt1.o 函数 _start 中未定义对“main”错误的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5646457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:36:05  来源:igfitidea点击:

Undefined reference to 'main' error in crt1.o function _start

c++cmakefile

提问by Rayne

I've having problems with my Makefile.

我的 Makefile 有问题。

I'm trying to create a program from 2 files - main.cpp that contains the main function, and modules.c that contains the definitions of the functions that are called in main(). modules.c only contain function definitions, no main function.

我正在尝试从 2 个文件创建一个程序 - main.cpp 包含主函数,modules.c 包含 main() 中调用的函数的定义。modules.c 只包含函数定义,没有主函数。

My Makefile is as follows:

我的 Makefile 如下:

CC := gcc
CXX := g++
LINK := g++ -Wall
CFLAGS := -g
CXXFLAGS := -g

TARGET = program

$(TARGET): modules.o main.o
   $(LINK) -o $@ $< -lpcap

clean:
   rm *.o $(TARGET)

modules.o:
   $(CC) $(CFLAGS) -c modules.c -o $@ $<

main.o:
   $(CXX) $(CXXFLAGS) -c main.cpp -o $@ $<

I have included "modules.h", which contains all the function declarations, in my main.cpp. The CFLAGS and CXXFLAGS variables point to the correct paths containing

我在 main.cpp 中包含了包含所有函数声明的“modules.h”。CFLAGS 和 CXXFLAGS 变量指向包含以下内容的正确路径

When I try to make using this Makefile, I get the error

当我尝试使用此 Makefile 进行制作时,出现错误

/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../lib64/crt1.o: In function '_start':
(.text+0x20): undefined reference to 'main'

/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../lib64/crt1.o:在函数'_start'中:(.
text+0x20):未定义的引用'主要'

If I switch the order of modules.o and main.o in my $(TARGET) line, then I get errors that say "undefined reference to" the functions I have defined in modules.c, in main.cpp.

如果我在 $(TARGET) 行中切换 modules.o 和 main.o 的顺序,那么我会收到错误消息,指出“未定义引用”我在 main.cpp 中的 modules.c 中定义的函数。

I don't know what is wrong.

我不知道出了什么问题。

Thank you.

谢谢你。

Regards, Rayne

问候, 雷恩

采纳答案by tux21b

Here are a couple of hints:

这里有一些提示:

  1. -o $@ $<is not needed for .o files, so remove that from those targets.
  2. -Wallmakes more sense when used while compiling not linking. So I would add it to the CCand CXXline instead (or better, to the CFLAGSand CXXFLAGS).
  3. the clean target should be a dependency of the .PHONYtarget, so that you can execute it always (without previous check for changed dependencies).
  1. -o $@ $<.o 文件不需要,因此从这些目标中删除它。
  2. -Wall在编译而不是链接时使用更有意义。所以我会将它添加到CCandCXX行(或者更好,添加到CFLAGSand CXXFLAGS)。
  3. clean 目标应该是目标的依赖项.PHONY,以便您可以始终执行它(无需事先检查已更改的依赖项)。

If you still get an error about missing references to your functions from modules.c, you are probably missing some extern "C" ...statements in main.cpp. That's because the internal name of C++ functions is calculated differently than that from C functions (i think C++ prefixes all names with the namespace, class names, etc). To tell C++ that a specific function can be found using the old internal name for linkage, use the extern "C" statement.

如果您仍然收到关于从 modules.c 中丢失对您的函数的引用的错误,您可能extern "C" ...在 main.cpp 中丢失了一些语句。那是因为 C++ 函数的内部名称的计算方式与 C 函数的内部名称不同(我认为 C++ 在所有名称前面加上命名空间、类名等)。要告诉 C++ 可以使用用于链接的旧内部名称找到特定函数,请使用 extern "C" 语句。

回答by Patrick Georgi

Use $^ instead of $<. The latter contains only the first dependency (modules.o), so main.o is not linked into the executable.

使用 $^ 而不是 $<。后者仅包含第一个依赖项 (modules.o),因此 main.o 未链接到可执行文件中。