c++ - 不规则的 Makefile 行为?

标签 c++ makefile g++

我正在编译一个包含 3 个源文件的程序:ma​​in.cppDataHandler.cppDataHandler.h

我使用包含以下代码的 makefile 来构建:

生成文件

OBJECTS = main.o DataHandler.o
CC = g++
DEBUG = -g
CFLAGS = -c -std=c++11
LFLAGS = -lopendnp3 -lopenpal -lasiodnp3 -lasiopal -lpthread -std=c++11

lvoutstation : $(OBJECTS)
        $(CC) $(LFLAGS) $(OBJECTS) -o lvoutstation

main.o : main.cpp DataHandler.h
        $(CC) $(CFLAGS) main.cpp

DataHandler.o : DataHandler.cpp DataHandler.h
        $(CC) $(CFLAGS) DataHandler.cpp

.PHONY : clean
clean :
        rm lvoutstation $(OBJECTS)

它可以很好地构建 main.o 和 DataHandler.o,但是当它开始编译操作可执行文件时,它会给我所有的 asiodnp3 和 opendnp3 命名空间函数/类调用提供链接错误。

当我运行以下命令时:

g++ -o lvoutstation main.o DataHandler.o -lopendnp3 -lopenpal -lasiodnp3

-lasiopal -lpthread -std=c++11

工作正常..

我不明白链接错误从何而来?

编辑

请求错误信息时:

g++ -lopendnp3 -lopenpal -lasiodnp3 -lasiopal -std=c++11 main.o DataHandler.o -o lvoutstation
main.o: In function `main':
main.cpp:(.text+0x85): undefined reference to `asiodnp3::DNP3Manager::DNP3Manager(unsigned int, openpal::ICryptoProvider*, std::function<void ()>, std::function<void ()>)'
main.cpp:(.text+0xb7): undefined reference to `asiodnp3::DNP3Manager::AddLogSubscriber(openpal::ILogHandler*)'
main.cpp:(.text+0xf0): undefined reference to `opendnp3::ChannelRetry::Default()'
main.cpp:(.text+0x123): undefined reference to `asiodnp3::DNP3Manager::AddTCPServer(char const*, unsigned int, opendnp3::ChannelRetry const&, std::string const&, unsigned short)'
main.cpp:(.text+0x19f): undefined reference to `opendnp3::EventBufferConfig::AllTypes(unsigned short)'
main.cpp:(.text+0x1d6): undefined reference to `opendnp3::DefaultOutstationApplication::Instance()'
main.cpp:(.text+0x2c8): undefined reference to `asiodnp3::DNP3Manager::~DNP3Manager()'
main.o: In function `opendnp3::LinkConfig::LinkConfig(bool, bool)':
main.cpp:(.text._ZN8opendnp310LinkConfigC2Ebb[_ZN8opendnp310LinkConfigC5Ebb]+0x75): undefined reference to `openpal::TimeDuration::Seconds(long)'
main.cpp:(.text._ZN8opendnp310LinkConfigC2Ebb[_ZN8opendnp310LinkConfigC5Ebb]+0x87): undefined reference to `openpal::TimeDuration::Minutes(long)'
main.o: In function `opendnp3::OutstationConfig::OutstationConfig()':
main.cpp:(.text._ZN8opendnp316OutstationConfigC2Ev[_ZN8opendnp316OutstationConfigC5Ev]+0x14): undefined reference to `opendnp3::OutstationParams::OutstationParams()'
main.cpp:(.text._ZN8opendnp316OutstationConfigC2Ev[_ZN8opendnp316OutstationConfigC5Ev]+0x56): undefined reference to `opendnp3::EventBufferConfig::EventBufferConfig(unsigned short, unsigned short, unsigned short, unsigned short, unsigned short, unsigned short, unsigned short, unsigned short)'
main.o: In function `asiodnp3::ConsoleLogger::Instance()':
main.cpp:(.text._ZN8asiodnp313ConsoleLogger8InstanceEv[_ZN8asiodnp313ConsoleLogger8InstanceEv]+0x5): undefined reference to `asiodnp3::ConsoleLogger::instance'
DataHandler.o: In function `DataHandler::ReadMeasurements(asiodnp3::IOutstation*)':
DataHandler.cpp:(.text+0xfe): undefined reference to `asiodnp3::MeasUpdate::MeasUpdate(asiodnp3::IOutstation*)'
DataHandler.cpp:(.text+0x11d): undefined reference to `opendnp3::Analog::Analog(double)'
DataHandler.cpp:(.text+0x137): undefined reference to `asiodnp3::MeasUpdate::Update(opendnp3::Analog const&, unsigned short, opendnp3::EventMode)'
DataHandler.cpp:(.text+0x159): undefined reference to `asiodnp3::MeasUpdate::~MeasUpdate()'
DataHandler.cpp:(.text+0x17b): undefined reference to `asiodnp3::MeasUpdate::~MeasUpdate()'
collect2: error: ld returned 1 exit status
make: *** [lvoutstation] Error 1

最佳答案

生成文件按预期工作。您的操作实际上与您的命令行示例不同

 $(CC) $(LFLAGS) $(OBJECTS) -o lvoutstation

将扩展为

 g++ -lopendnp3 -lopenpal -lasiodnp3 -lasiopal -lpthread -std=c++11 \ 
     main.o DataHandler.o -o lvoutstation

目标文件和库的顺序对链接过程很重要。

您可以修复链接器错误,在目标文件之后提供库:

 $(CC) $(OBJECTS) $(LFLAGS) -o lvoutstation

关于c++ - 不规则的 Makefile 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32890839/

相关文章:

c++ - 线程处理 C++ linux OS

c++ - make clean 结果没有具有特定 makefile 名称的目标

gcc - 在gcc和g++中选择消息语言

c++ - G++未定义对类::函数的引用

c++ - OpenCV程序中出现"xxx does not named a type"错误

c++ - 如何交叉编译具有依赖项的 C++ 库?

c++ - 错误 : No instance of constructor matches the argument list

c++ - Qt Creator closeEvent问题

makefile - 在GNU Make中使用ifeq和ifndef

bash - 为什么我要做 Cmake .. 并制作?