在Linux中使用针对C/C++的Makefiles构建和链接

Build and Link using Makefiles for C/C++ in Linux

本文关键字:C++ Makefiles 构建 链接 Linux      更新时间:2023-10-16

下面是我的makefile

#Makefile for beaglebone
#General tools
CC=gcc
CFLAGS = -g -Wall
RM = rm -fr
TARGET = beaglebone
# Source locations
BACNET_CORE = ../../src
BACNET_INCLUDE = ../../include
BACNET_HANDLER = ../../demo/handler
BACNET_OBJECT = ../../demo/object
BACNET_DEMO = ../../demo
# local files for this project
CSRC = main.c  arcnet.c  bip-init.c  dlmstp.c  dlmstp_linux.c ethernet.c rs485.c timer.c device.c
#common demo files needed
DEMOSRC = $(BACNET_DEMO)/handler/txbuf.c $(BACNET_DEMO)/handler/h_npdu.c $(BACNET_DEMO)/handler/s_iam.c $(BACNET_DEMO)/handler/noserv.c
# core BACnet stack files
CORESRC =  $(BACNET_CORE)/crc.c $(BACNET_CORE)/npdu.c $(BACNET_CORE)/bacdcode.c $(BACNET_CORE)/bacint.c $(BACNET_CORE)/bacreal.c 
    $(BACNET_CORE)/bacstr.c $(BACNET_CORE)/iam.c $(BACNET_CORE)/rp.c $(BACNET_CORE)/wp.c $(BACNET_CORE)/whois.c $(BACNET_CORE)/bacaddr.c  
        $(BACNET_CORE)/abort.c  $(BACNET_CORE)/reject.c $(BACNET_CORE)/bacerror.c  $(BACNET_CORE)/bacapp.c
## Include Directories
INCLUDES = -I. -I$(BACNET_INCLUDE)
INCLUDES += -I$(BACNET_OBJECT)
INCLUDES += -I$(BACNET_HANDLER)
# Source to Object conversion
COBJ = $(CSRC:.c=.o)
DEMOOBJ = $(DEMOSRC:.c=.o)
COREOBJ = $(CORESRC:.c=.o)
OBJECTS = $(COBJ) $(DEMOOBJ) $(COREOBJ)
#Build and Link the objects
all: $(TARGET)
.c.o:
    $(CC) -c $(INCLUDES) $(CFLAGS) $*.c -o $@
$(TARGET) : $(OBJECTS)
    $(CC) $(CFLAGS) $(INCLUDES) $(OBJECTS) -o  $(TARGET) 
.PHONY: clean
clean:
    $(RM) *.o *~ $(TARGET)

当问题出现时,我会创建.o文件,但不会创建可执行文件。我得到的输出如下:

s_iam.c:(.text+0xed): undefined reference to `bip_get_my_address'
../../demo/handler/s_iam.o: In function `Send_I_Am_Unicast':
s_iam.c:(.text+0x16c): undefined reference to `bvlc_send_pdu'
../../demo/handler/noserv.o: In function `handler_unrecognized_service':
noserv.c:(.text+0x14): undefined reference to `bip_get_my_address'
noserv.c:(.text+0x6b): undefined reference to `bvlc_send_pdu'
../../src/bacapp.o: In function `bacapp_copy':
bacapp.c:(.text+0x7ac): undefined reference to `datetime_copy_date'
bacapp.c:(.text+0x7bb): undefined reference to `datetime_copy_time'
../../src/bacapp.o: In function `bacapp_snprintf_value':
bacapp.c:(.text+0xba4): undefined reference to `bactext_object_type_name'
bacapp.c:(.text+0xbbc): undefined reference to `bactext_event_state_name'
bacapp.c:(.text+0xbce): undefined reference to `bactext_engineering_unit_name'
bacapp.c:(.text+0xbe1): undefined reference to `bactext_binary_polarity_name'
bacapp.c:(.text+0xbf0): undefined reference to `bactext_binary_present_value_name'
bacapp.c:(.text+0xbfa): undefined reference to `bactext_reliability_name'
bacapp.c:(.text+0xc04): undefined reference to `bactext_device_status_name'
bacapp.c:(.text+0xc0e): undefined reference to `bactext_segmentation_name'
bacapp.c:(.text+0xc18): undefined reference to `bactext_node_type_name'
bacapp.c:(.text+0xc45): undefined reference to `bactext_day_of_week_name'
bacapp.c:(.text+0xc84): undefined reference to `bactext_month_name'
bacapp.c:(.text+0xe28): undefined reference to `bactext_object_type_name'
../../src/bacapp.o: In function `bacapp_parse_application_data':
bacapp.c:(.text+0x10bd): undefined reference to `datetime_set_date'
collect2: error: ld returned 1 exit status
make: *** [beaglebone] Error 1

请告诉我哪里出了问题,非常感谢你的帮助和建议!!!

谨致问候,Gibson

看起来对象文件的顺序有点随机,所以后面的对象文件引用了前面对象文件中定义的符号,这就是链接失败的原因。

请尝试以下操作。替换:

$(TARGET) : $(OBJECTS)
    $(CC) $(CFLAGS) $(INCLUDES) $(OBJECTS) -o  $(TARGET) 

带有:

$(TARGET) : $(OBJECTS)
    $(CC) -o $@ -Wl,--start-group $(OBJECTS) -Wl,--end-group

这将使ld扫描对象文件几次丢失的符号(就像MSVC一样)。