Linux – Cannot be compiled with g++

Cannot be compiled with g++… here is a solution to the problem.

Cannot be compiled with g++

Now I try to use an open-source tool called “GAUL”, which implements some genetic algorithms. By compiling the sample file, I found that I could only compile this data by using gcc instead of g++. For example:


1) Use gcc -I/usr/local/include/-c wildfire_threat.c -o test.o

gcc** -g -O2 -Wall -o test2.out test.o -lgaul -lgaul_util -lm -lpthread -lslang -lm

Also suitable for combinations

gcc -I/usr/local/include/-c wildfire_threat.c -o test.o

g++ -g -O2 -Wall -o test2.out test.o -lgaul -lgaul_util -lm -lpthread -lslang -lm


But 2) use

g++ -I/usr/local/include/-c wildfire_threat.c -o test.o

g++ -g -O2 -Wall -o test2.out test.o -lgaul -lgaul_util -lm -lpthread -lslang -lm

I get the following error message:

test.o: In function `wildfire_simulation(int*, bool)':
wildfire_threat.c:(.text+0x52): undefined reference to `random_int(unsigned int)'
wildfire_threat.c:(.text+0x74): undefined reference to `random_int(unsigned int)'
test.o: In function `wildfire_score(population_t*, entity_t*)':
wildfire_threat.c:(.text+0xb55): undefined reference to `ga_entity_set_fitness(entity_t*, double)'
test.o: In function `wildfire_seed(population_t*, entity_t*)':
wildfire_threat.c:(.text+0xb86): undefined reference to `random_boolean()'
wildfire_threat.c:(.text+0xbb9): undefined reference to `random_boolean_prob(double)'
wildfire_threat.c:(.text+0xc18): undefined reference to `random_int(unsigned int)'
wildfire_threat.c:(.text+0xc25): undefined reference to `random_int(unsigned int)'
wildfire_threat.c:(.text+0xc92): undefined reference to `random_int(unsigned int)'
test.o: In function `wildfire_mutate_flip(population_t*, entity_t*, entity_t*)': 
wildfire_threat.c:(.text+0xd41): undefined reference to `random_int(unsigned int)'
test.o: In function `wildfire_crossover(population_t*, entity_t*, entity_t*, entity_t*, entity_t*)':
wildfire_threat.c:(.text+0xe25): undefined reference to `random_boolean()' 
wildfire_threat.c:(.text+0xe37): undefined reference to `random_int(unsigned int)' 
wildfire_threat.c:(.text+0xff0): undefined reference to `random_int(unsigned int)'
test.o: In function `wildfire_ga_callback(int, population_t*)':
wildfire_threat.c:(.text+0x11bc): undefined reference to `ga_get_entity_from_rank(population_t*, unsigned int)'
wildfire_threat.c:(.text+0x11c4): undefined reference to `ga_entity_get_fitness(entity_t*)'
wildfire_threat.c:(.text+0x11ef): undefined reference to 
`ga_population_score_and_sort(population_t*)' 
wildfire_threat.c:(.text+0x1206): undefined reference to `ga_fitness_mean_stddev(population_t*, double*, double*)'
wildfire_threat.c:(.text+0x122b): undefined reference to `ga_get_entity_from_rank(population_t*, unsigned int)'
wildfire_threat.c:(.text+0x1233): undefined reference to `ga_entity_get_fitness(entity_t*)'
wildfire_threat.c:(.text+0x1268): undefined reference to `ga_get_entity_from_rank(population_t*, unsigned int)'
wildfire_threat.c:(.text+0x1270): undefined reference to `ga_entity_get_fitness(entity_t*)'
test.o: In function `main':
wildfire_threat.c:(.text+0x12b0): undefined reference to `random_seed(unsigned int)'
wildfire_threat.c:(.text+0x12dd): undefined reference to `ga_select_two_roulette_rebased(population_t*, entity_t**, entity_t**)'
wildfire_threat.c:(.text+0x12e6): undefined reference to `ga_select_one_roulette_rebased(population_t*, entity_t**)'
wildfire_threat.c:(.text+0x132e): undefined reference to `ga_genesis_integer(int, int, int, bool (*)(int, population_t*), bool (*)(int, entity_t*), void (*)(void*), void (*)(void*), bool (*)(population_t*, entity_t*), bool (*)(population_t*, entity_t*), entity_t* (*)(population_t*, entity_t*), bool (*)(population_t*, entity_t**), bool (*)(population_t*, entity_ t**, entity_t**), void (*)(population_t*, entity_t*, entity_t*), void (*)(population_t*, entity_t*, entity_t*, entity_t*, entity_t*), void (*)(population_t*, entity_t*), void*)'
wildfire_threat.c:(.text+0x135c): undefined reference to `ga_population_set_parameters(population_t*, ga_scheme_type_t, ga_elitism_type_t, double, double, double)'
wildfire_threat.c:(.text+0x136d): undefined reference to `ga_evolution_threaded(population_t*, int)'
wildfire_threat.c:(.text+0x137e): undefined reference to `ga_get_entity_from_rank(population_t*, unsigned int)'
wildfire_threat.c:(.text+0x1386): undefined reference to `ga_entity_get_fitness(entity_t*)'
wildfire_threat.c:(.text+0x13b5): undefined reference to `ga_get_entity_from_rank(population_t*, unsigned int)'
wildfire_threat.c:(.text+0x1434): undefined reference to `ga_get_entity_from_rank(population_t*, unsigned int)'
wildfire_threat.c:(.text+0x1528): undefined reference to `ga_extinction(population_t*)'
collect2: ld returned 1 exit status

Can anyone explain to me the mystery and how I can avoid using gcc as this would cause problems for the remaining source code I want to embed?

Solution

Your third-party C library may be missing the extern "C" declaration in its header. To work around this without modifying the third-party header, you can do something similar in your C++ source code, wherever you #include the relevant third-party header:

extern "C" {
  #include "gaul.h"         // note: I'm just guessing the names of the
  #include "gaul_utils.h"   //       actual header files here...
}

Related Problems and Solutions