我的问题似乎很简单,但是在谷歌搜索寻找解决方案时,我没有得到一个:-(
我正在编写一个交叉编译的 C 程序以在嵌入式 Linux 上运行。在其中,我需要在两个不同的文件中使用常量结构。 所以我在一个.h文件中定义了它:
const s_GroupData Grp_Para_loivariable_Data ={
.groupId = GRP_PARA_LOIVARIABLE,
.groupAddress = GRP_PARA_LOIVARIABLE_ADDRESS,
.dataNr = 4,
.dataList =
{
{"service:heating_circuit:1:heating_curve:variable:temp_ext", 12, NegateMsbDivideBy10AndConvertCelsiusToK, ConvertKelvinToCelsiusMultiplyBy10AndSetMsb},
{"service:heating_circuit:1:heating_curve:variable:temp_int", 13, DivideBy10AndConvertCelsiusToK, ConvertKelvinToCelsiusAndMultiplyBy10},
{"service:heating_circuit:1:heating_curve:variable:mod_set", 14, NegateMsbDivideBy10AndConvertCelsiusToK, ConvertKelvinToCelsiusMultiplyBy10AndSetMsb},
{"service:heating_circuit:1:heating_curve:variable:mod_dt", 15, NegateMsbDivideBy10AndConvertCelsiusToK, ConvertKelvinToCelsiusMultiplyBy10AndSetMsb},
}
};
。我将这个文件包含在需要Grp_Para_loivariable_Data(BIO_high_param_read.c(的文件之一中。在另一个文件(BIO_set_params.c(中,我只将Grp_Para_loivariable_Data声明为extern:
extern const s_GroupData Grp_para_loivariable_Data;
有了这个,我在为目标构建可执行文件时没有错误...... ...但是我也在编译和链接相同的文件,以便在主机上使用c ++测试框架进行模块测试(debian linux(。并且链接器找不到我的常数:
BIO_setparams.o : In the function « ChangeVariableHeatingCurveParams » :
BIO_set_params.c:(.text+0x6ea) : undefined reference to « Grp_para_loivariable_Data »
BIO_setparams.o:(.data.rel+0x0) : undefined reference to « Grp_para_loivariable_Data »
collect2: error: ld returned 1 exit status
Makefile:62 : recipe for target « TestBIO » failed
我试图在测试文件的 BIO_set_params.c、BIO_set_params.h 和 .h 中将此常量声明为 extern(之前使用 extern"C",因为我的测试是用 C++ 编写的(。但是我仍然收到此链接器错误。
这是我的制作文件:
# NATIVE COMPILERS
CC=gcc
CP=c++
PROJECT_SRC_DIR= ../src
PROJECT_INC_DIR= ../headers
# Project flags
P_CPPFLAGS=-I$(PROJECT_INC_DIR) -I$(PROJECT_SRC_DIR)/LIB -I$(PROJECT_SRC_DIR)/BIO
## Tests
TEST_SRC_DIR=./src
TEST_INC_DIR=./include
TEST_H_DIR=./headers
TEST_LIB_DIR=./lib`
# Tests flags
T_CPPFLAGS= -Wno-return-type -I$(TEST_INC_DIR) -I$(TEST_H_DIR)
LDFLAGS= -lgtest -l:libiniparser.so.0 -Wl,-rpath,./lib -lpthread -ljansson -lcrypto -lcurl -lhashmap -L$(TEST_LIB_DIR) -lhiredis -levent -lgcov
CFLAGS= -fprofile-arcs -ftest-coverage --coverage
# Executables
LIB= TestLIB
BIO= TestBIO
# Colors
GREEN = 33[0;32m
RED = 33[0;31m
WHITE = 33[0m
BLUE= 33[34m
all: data-model-gen $(BIO) allprint
# RunAll
runTests:
@../$(BIO) || exit 1;
# LIB
lib_redis_if.o: $(PROJECT_SRC_DIR)/LIB/LIB_redis_if.c
@$(CC) -o $@ -c $< -DTESTS $(T_CPPFLAGS) $(P_CPPFLAGS) --coverage && echo "$(BLUE)$(CC) $^$(WHITE)"
lib_conversion.o: $(PROJECT_SRC_DIR)/LIB/LIB_conversion.c
@$(CC) -o $@ -c $< -DTESTS $(T_CPPFLAGS) $(P_CPPFLAGS) --coverage && echo "$(BLUE)$(CC) $^$(WHITE)"
lib_modbus.o: $(PROJECT_SRC_DIR)/LIB/LIB_modbus_tcp_sync.c
@$(CC) -o $@ -c $< -DTESTS $(T_CPPFLAGS) $(P_CPPFLAGS) && echo "$(BLUE)$(CC) $^$(WHITE)"
# BIO
$(BIO): main.o BIO_SN.o tbio.o BIO_data_read.o lib_modbus.o lib_redis_if.o lib_conversion.o BIO_main.o BIO_setparams.o BIO_version_nr_read.o BIO_high_param_read.o
@$(CP) -o $@ $^ $(LDFLAGS) -lhiredis -lmodbus && echo "$(GREEN)BIO tests successfully created. Run './$@'$(WHITE)n"
BIO_SN.o: $(PROJECT_SRC_DIR)/BIO/BIO_SN.c
@$(CC) -o $@ -c $< -DTESTS $(P_CPPFLAGS) $(T_CPPFLAGS) --coverage && echo "$(BLUE)$(CC) $^$(WHITE)"
BIO_data_read.o: $(PROJECT_SRC_DIR)/BIO/BIO_data_read.c
@$(CC) -o $@ -c $< -DTESTS $(P_CPPFLAGS) $(T_CPPFLAGS) --coverage && echo "$(BLUE)$(CC) $^$(WHITE)"
BIO_main.o: $(PROJECT_SRC_DIR)/BIO/BIO_main.c
@$(CC) -o $@ -c $< -DTESTS $(P_CPPFLAGS) $(T_CPPFLAGS) && echo "$(BLUE)$(CC) $^$(WHITE)"
BIO_setparams.o: $(PROJECT_SRC_DIR)/BIO/BIO_set_params.c
@$(CC) -o $@ -c $< -DTESTS $(P_CPPFLAGS) $(T_CPPFLAGS) && echo "$(BLUE)$(CC) $^$(WHITE)"
BIO_version_nr_read.o: $(PROJECT_SRC_DIR)/BIO/BIO_version_nr_read.c
@$(CC) -o $@ -c $< -DTESTS $(P_CPPFLAGS) $(T_CPPFLAGS) && echo "$(BLUE)$(CC) $^$(WHITE)"
BIO_high_param_read.o: $(PROJECT_SRC_DIR)/BIO/BIO_high_param_read.c
@$(CC) -o $@ -c $< -DTESTS $(P_CPPFLAGS) $(T_CPPFLAGS) && echo "$(BLUE)$(CC) $^$(WHITE)"
tbio.o: $(TEST_SRC_DIR)/T_bio.cpp
@$(CP) -o $@ -c $< $(T_CPPFLAGS) $(P_CPPFLAGS) && echo "$(BLUE)$(CP) $^$(WHITE)"
# MAIN
main.o: $(TEST_SRC_DIR)/main.cpp
@$(CP) -o $@ -c $< $(P_CPPFLAGS) $(T_CPPFLAGS) && echo "$(BLUE)$(CP) $^$(WHITE)"
知道出了什么问题吗?
这是大写错误,你的变量是Grp_Para_loivariable_Data
的,但你的 extern 在"para"上用小写的 pGrp_para_loivariable_Data