我在Mac机器上编译c ++代码.我使用终端使用python运行安装文件.当我编译代码来生成.so文件时,我收到以下错误:
python setup.py build
running build
running build_ext
building 'cec13_func' extension
gcc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -DNDEBUG -g -O3 -arch x86_64 -I/Applications/Canopy.app/appdata/canopy-1.5.4.3105.macosx-x86_64/Canopy.app/Contents/include/python2.7 -c cec13_func.cpp -o build/temp.macosx-10.6-x86_64-2.7/cec13_func.o
cec13_func.cpp:91:6: error: variable has incomplete type 'void'
void test_func(x, f, nx, mx, func_num);<br>
^
cec13_func.cpp:92:1: error: expected unqualified-id
{
^
2 errors generated.
error: command 'gcc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)
这是代码:
double x[6]={1,2,3,4,5,6};
double f[2]={0,0};
int nx = 2;
int mx = 3;
int func_num = 1;
void test_func(x, f, nx, mx, func_num)
{
int cf_num=10,i;
if (ini_flag==1)
{
if ((n_flag!=nx)||(func_flag!=func_num))
{
ini_flag=0;
}
}
if (ini_flag==0)
{
FILE *fpt;
char FileName[30];
free(M);
free(OShift);
free(y);
free(z);
free(x_bound);
y=(double *)malloc(sizeof(double) * nx);
z=(double *)malloc(sizeof(double) * nx);
x_bound=(double *)malloc(sizeof(double) * nx);
for (i=0; i<nx; i++)
x_bound[i]=100.0;
sprintf(FileName, "input_data/M_D%d.txt", nx);
fpt = fopen(FileName,"r");
if (fpt==NULL)
{
printf("\n Error: Cannot open input file for reading \n");
}
M=(double*)malloc(cf_num*nx*nx*sizeof(double));
if (M==NULL)
printf("\nError: there is insufficient memory available!\n");
for (i=0; i<cf_num*nx*nx; i++)
{
fscanf(fpt,"%Lf",&M[i]);
}
fclose(fpt);
fpt=fopen("input_data/shift_data.txt","r");
if (fpt==NULL)
{
printf("\n Error: Cannot open input file for reading \n");
}
OShift=(double *)malloc(nx*cf_num*sizeof(double));
if (OShift==NULL)
printf("\nError: there is insufficient memory available!\n");
for(i=0;i<cf_num*nx;i++)
{
fscanf(fpt,"%Lf",&OShift[i]);
}
fclose(fpt);
n_flag=nx;
func_flag=func_num;
ini_flag=1;
//printf("Function has been initialized!\n");
}
for (i = 0; i < mx; i++)
{
switch(func_num)
{
case 1:
sphere_func(&x[i*nx],&f[i],nx,OShift,M,0);
f[i]+=-1400.0;
break;
case 2:
ellips_func(&x[i*nx],&f[i],nx,OShift,M,1);
f[i]+=-1300.0;
break;
default:
printf("\nError: There are only 28 test functions in this test suite!\n");
f[i] = 0.0;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要一些建议.谢谢!
这个...
void test_func(x, f, nx, mx, func_num)
Run Code Online (Sandbox Code Playgroud)
...完全破:函数的参数需要指定两个数据类型(例如int,double,const char*)和标识符.后来使用这些标识符意味着它们是变量名而不是struct/ class/ union/ enum或typedef名称),因此它是缺少的类型.
进一步令人困惑的事情,文件顶部有:
double x[6]={1,2,3,4,5,6};
double f[2]={0,0};
int nx = 2;
int mx = 3;
int func_num = 1;
Run Code Online (Sandbox Code Playgroud)
它们与test_func函数参数具有相同的名称.也许你想把它们放在函数调用中test_func,然后在调用期间传递它们?或者 - 更简单但更不灵活 - 如果test_func只需要使用这些特定参数运行,您可以将它们放在test_func正文的开头,并将参数列表留空(即test_func()).
最后,您的文件扩展名是.cpp- 暗示C++,但您的编译行使用的gcc是C语言.代码本身看起来它只使用C++的C子集,而不是那意味着您无法使用它编译它g++.