gpt4 book ai didi

c++ - C++ Makefile错误 “implicit entry/start for main executable”

转载 作者:行者123 更新时间:2023-12-02 10:49:19 24 4
gpt4 key购买 nike

我的项目具有以下结构:

root
|____Makefile
|
|___src
| |____*.cpp
|
|___includes
| |___*.h
|
|___obj
|___tmp
|___bin

问题是当我尝试使我收到此错误时:
    g++ -std=c++11 obj/book.o -o bin/main
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我的main.cpp在src内部,比我有一个文件.h,我在其中声明一个struct和一些函数,以及一个.cpp,其中我定义了所有内容。在主要和CPP我做
#include "../includes/myfile.h"

我如何编译和解决此问题?
这是我的makefile:
CXX = g++
CXXFLAGS = -std=c++11 -I..
LDFLAGS = -std=c++11
EXECUTABLE= bin/main
SOURCES = $(wildcard src/*.cpp)
HEADERS = $(wildcard includes/*.h)
OBJECTS = $(patsubst src/%.cpp, obj/%.o, $(SOURCES))

BASE = $(USER)

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CXX) $(LDFLAGS) $< -o $@

obj/%.o: src/%.cpp
$(CXX) -c $(CXXFLAGS) $< -o $@

clean:
rm $(OBJECTS)

rmproper:
rm $(OBJECTS) $(EXECUTABLE)

pdf:
a2ps Readme.txt makefile $(HEADERS) $(SOURCES) -o /tmp/out.ps
ps2pdf /tmp/out.ps $(BASE).pdf

zip:
zip $(BASE).zip Readme.txt makefile $(HEADERS) $(SOURCES)

最佳答案

问题在这里:
$(EXECUTABLE): $(OBJECTS)
$(CXX) $(LDFLAGS) $< -o $@

$<是第一个前提条件,即$(OBJECTS)列表中的第一个目标文件(根据您的错误消息碰巧是book.o)。您要传递的是目标文件的整个列表,可以使用$^或什至$(OBJECTS)代替$<来完成。

关于c++ - C++ Makefile错误 “implicit entry/start for main executable”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52467395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com