+ -
当前位置:首页 → 问答吧 → 请教一个多线程静态库的问题, 谢谢

请教一个多线程静态库的问题, 谢谢

时间:2010-08-28

来源:互联网

本帖最后由 unicornx 于 2010-08-28 09:37 编辑

Windows 下做一个多线程的静态库 "/MT”

#include "stdafx.h"

#include <iostream>
using namespace std;

void c ( int errno )
{
}
int d ( )
{
    int ret = 0;
   
    c ( ret );  //此行会报错误
   
    return ret;
}

发现只要加上以下两行就编译不过:

#include <iostream>
using namespace std;

报错:error C2664: 'c' : cannot convert parameter 1 from 'int' to 'int *(__cdecl *)(void)'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

请问有谁碰到过这个问题,该如何解决?那两行还不能不要,应为我要使用C++的string。

作者: unicornx   发布时间: 2010-08-28

要用c++的string,不一定需要 using namespace std; 的,使用string时直接写 std::string ,或者将 using namespace std; 改成 using std::string

作者: drangon   发布时间: 2010-08-28

本帖最后由 unicornx 于 2010-08-28 09:36 编辑

回复 drangon
貌似和using namespace std;没有关系。
只要加上#include <iostream>就不行
如下这样写就报错:
#include "stdafx.h"
#include <iostream>
void c ( int errno )
{
}
int d ( )
{
    int ret = 0;
    c ( ret );
    return ret;
}
而且只和设置多线程有关,如果设置为单线程就编译ok。

作者: unicornx   发布时间: 2010-08-28

应该是需要声明的问题,你声明下c就好了
做lib 只编译不链接的

作者: phy0077   发布时间: 2010-08-28

回复 phy0077
试了这个,还是不行:(,一样的错误, 请问是我声明的方式不对还是别的问题?
#include <iostream>
using namespace std;

extern "C" {
    void a ( int errno );
    int b ( );
}

int b ( )
{
    int ret = 0;
    a ( ret );
    return ret;
}

void a ( int errno )
{
}

作者: unicornx   发布时间: 2010-08-28

本帖最后由 phy0077 于 2010-08-28 10:41 编辑

extern "C"
...


extern "C"你不会用吗

直接声明,不用extern "C"


.....


原来是errno的问题,换个名字,汗

作者: phy0077   发布时间: 2010-08-28

回复 phy0077


    谢谢各位,找到原因了:
void a ( int errno );
“errno" 是C++的保留变量名,自己定义的变量不能冲突,改成别的变量就好了。

结贴。

作者: unicornx   发布时间: 2010-08-28