My problems seems quite simple, but while googling to find a solution I did not get one :-(
I am writing a C program cross compiled to run on an embedded linux. In it, I need to use a constant structure in two different files. So I defined it in a .h file :
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},
}
};
... and I included this file to one of my files which needs Grp_Para_loivariable_Data (BIO_high_param_read.c). In the other file (BIO_set_params.c), I only declared Grp_Para_loivariable_Data as extern:
extern const s_GroupData Grp_para_loivariable_Data;
With this, I get no error while building my executable for my target... ... But I am also compiling and linking the same files for module testing on host (a debian linux), with a c++ test framework. And the linker can not find my constant :
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
I tried to declare this constant as extern in BIO_set_params.c, in BIO_set_params.h, and in .h of my test file (with extern "C" before, as my test is written in C++). But I am still getting this linker error.
Here is my makefile:
# 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 = \033[0;32m
RED = \033[0;31m
WHITE = \033[0m
BLUE= \033[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)"
Any idea of what is going wrong?