ubuntu:python调用c生成so文件出错

bfs.cpp:11:15: error: expected constructor, destructor, or type conversion before ‘(’ token
     __declspec(dllexport)

windows到linux的转换:

windows下:

#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

#define FF(a,b) for(a=0;a<b;a++)

extern "C" {
    __declspec(dllexport) 
	void fillHole(int * img,int X,int Y)
    ;
}

int x_s,y_s;

inline int MAP(int x,int y){
	return y_s*x + y;
}


int dx[4]={0, 0, 1, -1};
int dy[4]={1, -1, 0, 0};
int vis[1000*1000];

typedef struct Pt
{
	int x,y;
	Pt(int x,int y):x(x),y(y){
	}
}Pt;


bool legal(int x,int y){
	if(x<x_s && x>=0 && y<y_s && y>=0)
		return true;
	return false;
}

void bfs(int *img,int x,int y){
	queue<Pt> q;
	vector<Pt> v;
	q.push(Pt(x,y));
	bool flag=1;
	int i;
	while(!q.empty()){
		Pt pt=q.front();
		q.pop();
		vis[MAP(x,y)]=1;
		int cx=pt.x,cy=pt.y;
		FF(i,4){
			int tx=cx+dx[i];
			int ty=cy+dy[i];
			if(legal(tx,ty)){
				if(img[MAP(tx,ty)]==0 && vis[MAP(tx,ty)]==0){
					q.push(Pt(tx,ty));
					v.push_back(Pt(tx,ty));
					vis[MAP(tx,ty)]=1;
				}
			}else{
				flag=0;
			}
		}
		if(flag){
			int sz=v.size();
			FF(i,sz){
				int & tx=v[i].x;
				int & ty=v[i].y;
				img[MAP(tx,ty)]=255;
			}
		}
	}
}

void fillHole(int * img,int X,int Y){
	x_s=X,y_s=Y;
	int i,j;
	FF(i,x_s)FF(j,x_s)if(!vis[MAP(i,j)]){
		bfs(img,i,j);
	}
}

//int main() {
//	return 0;
//}

linux下的使用:

#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

#define FF(a,b) for(a=0;a<b;a++)

#if defined (__GNUC__) && defined(__unix__)
  #define PRINT_API __attribute__ ((__visibility__("default")))
#elif defined (WIN32)
  #ifdef BUILDING_DLL
    #define PRINT_API __declspec(dllexport)
  #else
    #define PRINT_API __declspec(dllimport)
  #endif
#endif
extern "C" PRINT_API void fillHole(int * img,int X,int Y);


#define FF(a,b) for(a=0;a<b;a++)

//extern "C" {
//   _declspec(dllexport)
//    void fillHole(int * img,int X,int Y)
//    ;
//}

int x_s,y_s;

inline int MAP(int x,int y){
	return y_s*x + y;
}


int dx[4]={0, 0, 1, -1};
int dy[4]={1, -1, 0, 0};
int vis[1000*1000];

typedef struct Pt
{
	int x,y;
	Pt(int x,int y):x(x),y(y){
	}
}Pt;


bool legal(int x,int y){
	if(x<x_s && x>=0 && y<y_s && y>=0)
		return true;
	return false;
}

void bfs(int *img,int x,int y){
	queue<Pt> q;
	vector<Pt> v;
	q.push(Pt(x,y));
	bool flag=1;
	int i;
	while(!q.empty()){
		Pt pt=q.front();
		q.pop();
		vis[MAP(x,y)]=1;
		int cx=pt.x,cy=pt.y;
		FF(i,4){
			int tx=cx+dx[i];
			int ty=cy+dy[i];
			if(legal(tx,ty)){
				if(img[MAP(tx,ty)]==0 && vis[MAP(tx,ty)]==0){
					q.push(Pt(tx,ty));
					v.push_back(Pt(tx,ty));
					vis[MAP(tx,ty)]=1;
				}
			}else{
				flag=0;
			}
		}
		if(flag){
			int sz=v.size();
			FF(i,sz){
				int & tx=v[i].x;
				int & ty=v[i].y;
				img[MAP(tx,ty)]=255;
			}
		}
	}
}

void fillHole(int * img,int X,int Y){
	x_s=X,y_s=Y;
	int i,j;
	FF(i,x_s)FF(j,x_s)if(!vis[MAP(i,j)]){
		bfs(img,i,j);
	}
}

//int main() {
//	return 0;
//}

reference:

https://www.cnblogs.com/TQCAI/p/8886983.html

发布了249 篇原创文章 · 获赞 198 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/104353385