在Linux中使用针对C/C++的Makefiles构建和链接
Build and Link using Makefiles for C/C++ in Linux
下面是我的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一样)。
相关文章:
- mingw32-make 使用"MinGW Makefiles"生成器跟踪 CMAKE 无法将可执行文件链接到对象库
- CMake & MinGW Compileilation on Windows,无需 -G "MinGW Makefiles"标志
- Cmake 3.8.0在makefiles中生成错误的链接命令
- CMake 找不到与 "Unix Makefiles" 对应的构建程序
- Maintaining Makefiles and CMakeLists.txt
- 如何在使用MingW和makefiles时链接到库
- 使用 Makefiles 在 Eclipse CDT 中进行错误检查
- 使用makefiles时如何禁用C .pdb生成
- 在Linux中使用针对C/C++的Makefiles构建和链接
- 在 suse10、"checking whether to enable maintainer-specific portions of Makefiles... no" 下安装 GCC 4.9
- Makefiles:转换文件扩展名
- 如何在使用makefiles时禁用GCC优化
- 隐式构建规则:GNU Make Multiple Makefiles Multiple Directories
- Makefiles:是否有一种通用的方法来显示makefile中的哪一行导致了错误
- 通过makefiles链接文件
- 继续使用g++和Makefiles从编译后的代码中打印编译器警告
- 在Makefiles中包含用于单元测试文件的gtest库:
- Qt在.pro文件更改后不刷新Makefiles
- 将Makefiles转换为CMAKEList(编译成功,但程序的行为不同)