mingw 编译报错

aocaopo 2011-03-27 06:23:51

#include <stdio.h>
#include<winsock2.h>
#include <time.h>
#pragma comment(lib,"ws2_32.lib")

char *host;
int threadnum,maxthread,totalport;
long nowport;


TIMEVAL timeout;
FD_SET mask;
void usage(char *name)
{
printf("\t===================Portscaner======================\n");
printf("\t============gxisone@hotmail.com 2004/7/6=======\n");
printf("\tusage: %s IP StartPort-EndPort MaxThread(1000)\n",name);
printf("\tExample: %s 192.168.1.1 1-10000 500\n",name);
}


void display(void) // 定义状态提示函数
{
static int play=0;
// 进度条
char *plays[12]=
{
" | ",
" / ",
" - ",
" \\ ",
" | ",
" / ",
" - ",
" // ",
" | ",
" / ",
" - ",
" // ",
};

printf("=%s=\t%d threads %d %s Completed. \r", plays[play],threadnum,nowport*100/(totalport+1),"%");
play=(play==11)?0:play+1;
Sleep(1);

}




void WaitThreadEnd(void)
{
Sleep(1000);
printf("\nThread ending....\n");
while(threadnum>0)
{
Sleep(1);
printf(" =|=\t%d threads \r",threadnum);


}

printf("\nThread ended!\n");
}

DWORD WINAPI ThreadFunc( LPVOID lp ) //线程函数,跟普通的函数没什么两样
{

int port=*(DWORD*)lp;

SOCKET sockfd;
struct sockaddr_in addr;


u_long value;
addr.sin_family =AF_INET;
addr.sin_addr.s_addr =inet_addr(host);

value=1;



sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd==INVALID_SOCKET)
{
printf("Socket error!\n");
InterlockedExchangeAdd((long *)&threadnum,-1);
return 0;
}
ioctlsocket(sockfd,FIONBIO,&value);
addr.sin_port = htons(port);


connect(sockfd,(struct sockaddr *) &addr, sizeof(addr));


FD_ZERO(&mask);
FD_SET(sockfd,&mask);
value=select(0,NULL,&mask,NULL,&timeout);
if(value==0 || value==-1)
{
closesocket(sockfd);
Sleep(50);
InterlockedExchangeAdd((long *)&threadnum,-1);
return 0;

}
else
{
shutdown(sockfd, 0);
printf("\t\tFound port %d open.\r\n",port);
closesocket(sockfd);
Sleep(50);
InterlockedExchangeAdd((long *)&threadnum,-1);
}



return 0;
}

VOID main( int argc,char **argv )
{

WSADATA ws;
char *p;
int startport,endport;
clock_t start,end;//程序运行的起始和结束时间
float costtime;//程序耗时



if(argc!=4)
{
usage(argv[0]);
return ;
}



long lresult;

lresult=WSAStartup(MAKEWORD(2,2), &ws);

p=argv[2];//处理端口参数
if(strstr(argv[2],"-"))
{ startport=atoi(argv[2]);
for(;*p;)
if(*(p++)=='-')break;
endport=atoi(p);

if(startport<1 || endport>65535)
{
printf("Port Error!\n");
return;
}
}
host=argv[1];
maxthread=(atoi(argv[3])-1>999)?999:atoi(argv[3])-1;
usage(argv[0]);

timeout.tv_usec=0;
if(maxthread>500)timeout.tv_sec=2;
else timeout.tv_sec=1;

start=clock();//开始计时

totalport=endport-startport;

for(int port=startport;port<endport;port++,nowport++,threadnum++)
{
display();
while(threadnum>maxthread)
{
Sleep(10);
}
CreateThread( NULL,0,ThreadFunc,&port, 0,NULL);
}

end=clock();//计时结束
costtime= (float)(end - start) / CLOCKS_PER_SEC; //转换时间格式
printf("Cost time:%f Sec",costtime);//显示耗时
printf("\n\n");
WaitThreadEnd();


WSACleanup();
return ;
}


================================================================
执行:gcc -o scan.exe gscan.cpp -lwsock32
错误:
gscan.cpp: In function 'void display()':
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp:47:1: warning: deprecated conversion from string constant to 'char*'
gscan.cpp: At global scope:
gscan.cpp:129:33: error: '::main' must return 'int'
gscan.cpp: In function 'int main(int, char**)':
gscan.cpp:143:1: error: return-statement with no value, in function returning 'i
nt'
gscan.cpp:162:13: error: return-statement with no value, in function returning '
int'
gscan.cpp:195:1: error: return-statement with no value, in function returning 'i
nt'

首先代码是没错误的 但是 编译的时候出错。mingw 编译经常出现这样那样的错误。。 求解
...全文
267 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
aocaopo 2011-03-28
  • 打赏
  • 举报
回复
感谢 楼上 的 。。。 这个代码找了 很久 ~~
hongwenjun 2011-03-28
  • 打赏
  • 举报
回复
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
"SelfRunDemo"="D:\\myapp\\readregedit\\bin\\Debug\\readregedit.exe"

自动把自己注册为 自动运行 GCC编译通过
///////////////////////////////////////////
// SelfRun.cpp文件

#include <stdio.h>
#include <windows.h>

int main(int argc, char* argv[]) // 09 SelfRun工程下
{
// 根键、子键名称、和到子键的句柄
HKEY hRoot = HKEY_LOCAL_MACHINE;
char *szSubKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hKey;

// 打开指定子键
DWORD dwDisposition = REG_OPENED_EXISTING_KEY; // 如果不存在不创建
LONG lRet = ::RegCreateKeyEx(hRoot, szSubKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if(lRet != ERROR_SUCCESS)
return -1;

// 得到当前执行文件的文件名(包含路径)
char szModule[MAX_PATH] ;
::GetModuleFileName (NULL, szModule, MAX_PATH);
// 创建一个新的键值,设置键值数据为文件名
lRet = ::RegSetValueEx(hKey, "SelfRunDemo", 0, REG_SZ, (BYTE*)szModule, strlen(szModule));
if(lRet == ERROR_SUCCESS)
{
printf(" 自动启动设置成功!\n");
}

// 关闭子键句柄
::RegCloseKey(hKey);

getchar();
return 0;
}
Lactoferrin 2011-03-28
  • 打赏
  • 举报
回复
这个和其他的没什么区别
aocaopo 2011-03-28
  • 打赏
  • 举报
回复
求给出 一段 MinGW 能编译的 读注册表代码实例。
实在没办法
aocaopo 2011-03-27
  • 打赏
  • 举报
回复
嗯 。多谢楼上指点
Lactoferrin 2011-03-27
  • 打赏
  • 举报
回复
如果代码没有用到c++的特性,把扩展名改为c
然后使main返回int
aocaopo 2011-03-27
  • 打赏
  • 举报
回复
搞得很头疼 -
aocaopo 2011-03-27
  • 打赏
  • 举报
回复
不晓得 如何修改才规范。
aocaopo 2011-03-27
  • 打赏
  • 举报
回复
vc 里面的代码 要修改 才能用 ?刚接触 mingw 感觉界面清爽 编译些简单的 代码很方便 。无奈 很容易报错误。。
xunxun 2011-03-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lactoferrin 的回复:]

不是直接在windows.h中定义的,在winnt.h中
[/Quote]
恩,mingw里也有,原来没用过大写的
Lactoferrin 2011-03-27
  • 打赏
  • 举报
回复
不是直接在windows.h中定义的,在winnt.h中
xunxun 2011-03-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lactoferrin 的回复:]

windows.h中VOID被定义成void
[/Quote]
VC的?mingw gcc的windows.h没有这个
Lactoferrin 2011-03-27
  • 打赏
  • 举报
回复
windows.h中VOID被定义成void
xunxun 2011-03-27
  • 打赏
  • 举报
回复
只能说代码太不规范,
#pragma comment(lib,"ws2_32.lib")
只能用于VC,对gcc无效。
VOID main是啥?返回int为规范,void也行,但会警告,但你写VOID是什么?
Lactoferrin 2011-03-27
  • 打赏
  • 举报
回复
main本来就应该return int

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧