c++ - Makefile 链接错误

标签 c++ makefile linker

我的生成文件:

CXX = g++
CXXFLAGS = -g -Wall -std=c++14
LDFLAGS = -lboost_system -lcrypto -lssl -lcpprest -lpthread

SRC := $(shell find . -name *.cpp)
OBJ = $(SRC:%.cpp=%.o)
BIN = run

all: $(BIN)

$(BIN): $(OBJ)
    $(CXX) $(LDFLAGS)

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

clean:
    find . -name *.o -delete
    rm -f $(BIN)

它扫描所有子目录中的所有文件 *.cpp 文件并创建相应的 *.o 文件。然后它尝试将所有内容链接到最终的二进制文件中,但出现以下错误。我不知道如何解决这个问题。

/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/../../../../lib/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'

目录结构:

Makefile
sources/
    directory1
        ...cpp
    directory2
        ...cpp
    ...
    main.cpp

main.cpp 内容:

#include <iostream>

#include <signal.h>

#include "application/application_launcher.hpp"

Alastriona::Application::Launcher launcher;

void shutdown(int signal);

int main(int argc, const char * argv[])
{
    struct sigaction sa;
    sa.sa_handler = &::shutdown;
    sa.sa_flags = SA_RESTART;
    sigfillset(&sa.sa_mask);

    sigaction(SIGTERM, &sa, NULL);
    sigaction(SIGQUIT, &sa, NULL);
    sigaction(SIGINT, &sa, NULL);

    launcher.loadArguments(argc, argv);
    launcher.loadConfiguration();
    launcher.startApplication();
}

void shutdown(int signal)
{
    launcher.stopApplication();
}

最佳答案

int main(int argc, const char * argv[])

是由于常量性导致的重载,which is not allowed, and considered ill formed §2按标准。您需要使用的签名是

int main(int argc, char * argv[])

编辑:您在尝试构建目标时没有使用任何先决条件。 你应该有

$(BIN): $(OBJ)
    $(CXX) $^ $(LDFLAGS)

关于c++ - Makefile 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48261575/

相关文章:

c++ - 模板 : get proper variable name from type name

c++ - 如何正确使用重载的 “operator<<”?

iphone - 静态 (iPhone) 库、分发和依赖项

c++ - OpenCV cvSmooth 链接器错误

c - 非标准目录布局中的 makefile

c++ - Visual C++ 链接器错误 2019

c++ - 如何使用 Code::Blocks 链接到库?

c++ - 什么是 "Argument-Dependent Lookup"(又名 ADL,或 "Koenig Lookup")?

c++ - 如何使用 Microsoft 链接器工具链接静态 MFC 库

makefile - Makefile Target `.c.o` 是做什么用的?