尝试在 c++ 程序中包含 btstack C 文件(.h 和 .c)时链接出现问题



我试图将L2CAP服务器(在C中-使用BlueKitchen的btstack)与UDP服务器(在c++中)集成。首先,我尝试将L2CAP服务器的代码添加到main.cpp"UDP服务器的文件。我使用extern "C" {...}包含了所有相关的C库和头文件。首先,我会在btstack文件的include语句上得到错误。我通过修改make文件来解决这个问题,使其包含"INC = -I./directory/…"接下来,我处理了c++编译器在粘贴的C代码中发现的问题(例如,在字面值和字符串之间添加空格)。在解决这些问题后,似乎没有一个块文件/函数被正确链接,因为这是我在尝试"make":

时得到的结果。"未定义引用' main.cpp中每个单独的btstack函数调用'

我的makefile。请注意,其中很多可能是不必要的/什么也没做。我只是尝试了所有的东西,并从bstack端口makefile中借用了几行,看看它们是否有效。


CORE += 
btstack_chipset_bcm.c 
btstack_chipset_bcm_download_firmware.c 
btstack_control_raspi.c 
btstack_link_key_db_tlv.c 
btstack_run_loop_posix.c 
btstack_tlv_posix.c 
btstack_uart_posix.c 
btstack_slip.c 
hci_dump_posix_fs.c 
hci_transport_h4.c 
hci_transport_h5.c 
le_device_db_tlv.c 
main.c 
wav_util.c 
btstack_stdin_posix.c 
raspi_get_model.c 
rijndael.c
.INCLUDE : home/pi/udpDemo/inc/btstack-master/example/Makefile.inc
CC=g++
CFLAGS=-Wall -Wextra -pedantic -std=c++11 
SRC=$(wildcard ./src/*.cpp)
OBJ=$(patsubst ./src/%.cpp, ./obj/%.o, $(SRC))
INC=-I./inc 
-I./inc/btsrc 
-I./inc/btsrc/ble 
-I./inc/btsrc/ble/gatt-service 
-I./inc/platform/embedded 
-I./inc/platform/posix 
-I./inc/chipset/bcm 
-I./inc/3rd-party/tinydir 
-I./inc/3rd-party/rijndael 
-I./inc/port/raspi 
-I./src
LDFLAGS += -lrt
VPATH += src
VPATH += inc/3rd-party/rijndael
VPATH += inc
VPATH += inc/btsrc 
VPATH += inc/btsrc/ble 
VPATH += inc/btsrc/ble/gatt-service 
VPATH += inc/platform/embedded 
VPATH += inc/platform/posix 
VPATH += inc/chipset/bcm 
VPATH += inc/3rd-party/tinydir 
VPATH += inc/3rd-party/rijndael 
VPATH += inc/port/raspi 

LIBS=
TARGET=test
bindir=/usr/local/bin
$(TARGET):$(OBJ)
$(CC)  -o $@ $^ $(CFLAGS) 
$(OBJ):./obj/%.o:./src/%.cpp
$(CC) $(CFLAGS) -c $^ -o $@ -I$(INC) 
clean:
rm -rf $(TARGET) $(OBJ)
install:$(TARGET)
install -g root -o root $(TARGET) $(bindir)/$(TARGET)
uninstall:$(bindir)/$(TARGET)
rm $(bindir)/$(TARGET)

看起来这里有makefile问题和compile问题。同时解决这两个问题很难。下面是makefile的简化和注释(但未经测试)版本,它可能是一个很好的起点。

你原来的makefile看起来像是包含了很多来自bstack构建系统的东西。出于技术和许可的原因,将两个独立的项目结合在一起通常不是一个好主意。使用第三方代码的典型方法是将该代码编译到库中,然后将代码链接到该库。关于如何做到这一点,你必须查阅btstack的文档,我不熟悉那个特定的库。

# Which compiler to use
CC=g++
# Options to pass to the compiler
CFLAGS=-Wall -Wextra -pedantic -std=c++11 
# Options to pass to the linker
LDFLAGS=-Wall -Wextra
LDFLAGS+=-lbtstack            # Link with 'libbtstack.a'
LDFLAGS+=-L/path/to/btstack   # Where to find the btstack libraries that were built separately
# Source files to compile (everything in 'src' directory)
SRC=$(wildcard src/*.cpp)
# Convert names of source files into their corresponding object files
OBJ=$(patsubst src/%.cpp, obj/%.o, $(SRC))
# Directories where headers are kept
INC=-Isrc
INC+=-I/path/to/btstack/headers
# Name of the program
TARGET=test
# Rule for linking the .o files together to build the program
$(TARGET): $(OBJ)
$(CC) $(LDFLAGS) -o $@ $^
# Rule for making a .o file from a .cpp file
obj/%.o: src/%.cpp
$(CC) $(CFLAGS) -c $^ -o $@ -I$(INC)
# Rule for deleting output and temporary files
clean:
rm -rf $(TARGET) $(OBJ)

这将编译src目录下的所有.cpp文件,并将它们与libbtstack.a链接以创建一个名为test的程序。在构建btstack库之后,用btstack库和头文件的实际路径分别更新LDFLAGSINC变量。

最新更新