简体   繁体   English

C ++在编译时未定义对“ main”的引用

[英]c++ undefined reference to `main' while compiling

After try to use this attached makefile the error that appears is : 尝试使用此附加的生成文件后,出现的错误是:

(.text+0x20): undefined reference to `main'

collect2: error: ld returned 1 exit status

make: *** [Ass1F] Error 1

the makefile is : makefile是:

all: Ass1F

Ass1F: bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
g++ -o bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o

@echo 'Finished building target: Ass1F'
@echo ' '

bin/x.o: src/x.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/x.o src/x.cpp

bin/y.o: src/y.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/y.o src/y.cpp

bin/z.o: src/z.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/z.o src/z.cpp

bin/w.o: src/w.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/w.o   src/w.cpp

bin/main.o: src/main.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/main.o src/main.cpp

clean: 
rm -f bin/*

what should I do with this problem? 我该怎么办? the reason is the makefile or something in the code? 原因是生成文件还是代码中的某些内容? just to let you know we used eclipse to write the code and everything work perfectly- no any bugs. 只是为了让您知道,我们使用eclipse编写了代码,并且一切正常运行-没有任何错误。

thanks 谢谢

Line g++ -o bin/main.o bin/xo bin/yo bin/zo bin/wo tries to create executable named bin/main.o , overwriting its previous contents. g++ -o bin/main.o bin/xo bin/yo bin/zo bin/wo尝试创建名为bin/main.o可执行文件,覆盖其先前的内容。

It should be eg g++ -o Ass1F bin/main.o bin/xo bin/yo bin/zo bin/wo 应该是例如g++ -o Ass1F bin/main.o bin/xo bin/yo bin/zo bin/wo

Your makefile recipe for Ass1F is incorrect, it doesn't specify the output file, os make uses bin/main.o as the output file instead of one of the input files. 您的Ass1F makefile配方不正确,它没有指定输出文件,os make使用bin/main.o作为输出文件而不是其中一个输入文件。

You can simplify the makefile by using pattern rules (ie with wildcards) and using pre-defined variables such as $@ (the name of the target) and $^ (the list of prerequisites) and CXX (the C++ compiler): 您可以使用模式规则(例如,使用通配符)以及使用预定义变量,例如$@ (目标名称)和$^ (前提条件列表)和CXX (C ++编译器)来简化makefile:

CXXFLAGS  = -g -Wall -Weffc++
CPPFLAGS =
LDFLAGS =
LDLIBS =

all: Ass1F

Ass1F: bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
        $(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)
        @echo 'Finished building target: Ass1F'
        @echo ' '

bin/%.o: src/%.cpp
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $^

clean: 
        rm -f bin/*

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

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