Linux 不能使用g++;编撰

Linux 不能使用g++;编撰,linux,gcc,compilation,g++,gaul,Linux,Gcc,Compilation,G++,Gaul,现在,我尝试使用一个名为“GAUL”的开源工具来实现一些遗传算法。通过编译示例文件,我发现我只能使用gcc编译这些数据,而不能使用g++。E.x.: 1) 使用gcc-I/usr/local/include/-cwildfire\u threat.c-o test.o gcc**-g-O2-Wall-o test2.out test.o-lgaul-lgaul_util-lm-lpthread-lslang-lm 也适用于组合 gcc-I/usr/local/include/-c野火威胁.c-

现在,我尝试使用一个名为“GAUL”的开源工具来实现一些遗传算法。通过编译示例文件,我发现我只能使用gcc编译这些数据,而不能使用g++。E.x.:


1) 使用
gcc-I/usr/local/include/-cwildfire\u threat.c-o test.o

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

也适用于组合

gcc-I/usr/local/include/-c野火威胁.c-o测试.o

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


但是2)使用

g++-I/usr/local/include/-c野火威胁.c-o测试.o

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

我收到以下错误消息:

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


有人能给我解释一下这个谜团吗?我如何避免使用gcc,因为这会给我想要嵌入的剩余源代码带来问题?

您的第三方C库的标题中可能缺少
extern“C”
声明。为了解决这个问题,不修改第三方头,你可以在C++源中做这样的事情,无论你在哪里,代码< >包含< /代码>相关的第三方标题:

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

<>你应该用C编译器和C++源编译C++源代码。在特定的情况下,高卢图书馆是用C编写的,它的头不适合C++编译,它们缺少“代码>外”“C”<代码>,所以当你编译为C++时,函数用C++链接声明,当然,在C编译库中找不到。

长短,你的源码是用C编写的。用C++编译器编译它。