不规则的Makefile行为?

时间:2015-10-01 15:02:34

标签: c++ makefile g++

我正在编写一个包含3个源文件的程序: main.cpp DataHandler.cpp DataHandler.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

1 个答案:

答案 0 :(得分:1)

makefile按预期工作。您的操作实际上与命令行示例

不同
 $(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