在Linux中使用Makefile为C / C ++构建和链接

时间:2015-02-26 08:42:57

标签: c++ c linux makefile

以下是我的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

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

此致 吉布森

1 个答案:

答案 0 :(得分:0)

看起来你的目标文件有点随机顺序,以便后面的目标文件引用早期目标文件中定义的符号,这就是链接失败的原因。

尝试以下方法。替换:

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

使用:

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

这将使ld扫描目标文件几次丢失符号(如MSVC那样)。