初来乍到,希望大家多多帮忙呀!lib中的函数如何调用?

greenabc 2004-11-09 08:38:49
我做了一个静态库,其中包含了一个类,类外有几个变量和函数,类和类内的成员可以访问,但类外的变量和函数不知如何访问。还有编译调用库的可执行文件时有如下的警告,如何消除警告。
LINK : warning LNK4098: defaultlib "LIBC" conflicts with use of other libs; use /NODEFAULTLIB:library
谢谢了!
...全文
237 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
definegg 2004-11-12
  • 打赏
  • 举报
回复
在Build菜单下,选择Build菜单下的Build mymath.lib。Visual C++编译链接工程,在mymath\debug目录下生成mymath.lib文件。至此,静态连接库生成的工作就做完了。下面用一个小程序来测试这个静态库。

哦,对了还有开始:

要创建静态库,选择File->New菜单,弹出New对话框。选择Projects标签,在项目类型列表框中选择Win32 Static Library,在Name中输入mymath,表明要创建一个mymath.lib的静态库文件。

然后用Project->Add to Project->Files菜单往mymath工程中加入上面的两个文件。

definegg 2004-11-12
  • 打赏
  • 举报
回复
1.头文件(见清单9.1):定义了Summary和Factorial两个函数,分别用于完成求和与阶乘。注意这里使用C风格的函数,需要加入extern “C”关键字,表明它是C风格的外部函数。

清单 头文件

#ifndef _MYMATH_H

#define _MYMATH_H

extern “C”

{

int Summary(int n);

int Factorial(int n);

}

#endif

2.源文件:包含了Summary和Factorial函数的定义,见清单9.2。

清单 源文件

int Summary(int n)

{

int sum=0;

int i;

for(i=1;i<=n;i++)

{

sum+=i;

}

return sum;

}

int Factorial(int n)

{

int Fact=1;

int i;

for(i=1;i<=n;i++)

{

Fact=Fact*i;

}

return Fact;

}

definegg 2004-11-12
  • 打赏
  • 举报
回复
关注!
definegg 2004-11-11
  • 打赏
  • 举报
回复
关注!
greenabc 2004-11-11
  • 打赏
  • 举报
回复
困惑中,请大家帮忙呀!
lzf6633861 2004-11-11
  • 打赏
  • 举报
回复
解决了?发短信QQ:276939874
lzf6633861 2004-11-11
  • 打赏
  • 举报
回复
加入类的头文件中加入:
#include "你调用的库的头文件.h"
#pragma comment(lib ,"你的库文件。LIB
int __declspec((__stdcall ) m_pubint;
int __declspec((__stdcall ) GetPubint()
这样看行不行。
忠向 2004-11-11
  • 打赏
  • 举报
回复
__declspec(dllimport)
应该放在返回值的前面吧
sinall 2004-11-11
  • 打赏
  • 举报
回复
m_LibClass.SetAge(int __declspec(dllimport) GetPubint() +10);
是肯定错误的
m_LibClass.SetAge(GetPubint() +10);试试
定义的时候需要 __declspec(dllimport)
使用的时候就不需要了。

greenabc 2004-11-11
  • 打赏
  • 举报
回复
没解决呀!拜托大家回答时,要有点把握呀!
是静态库,不要搞混了不是动态库!
greenabc 2004-11-11
  • 打赏
  • 举报
回复
另外又有个新的问题:

从CWnd继承的类的实例,如何取的其DC,并在别的窗体上显示出来且在该实例上画线等操作?
我做的只要一画线就出现错误,然后程序就退出了。分不够另给!!!
greenabc 2004-11-10
  • 打赏
  • 举报
回复
puregrid(puregrid):
1、静态库里的函数是全局的,只要在应用中包含相应声明的头文件,直接调用就行了;
2、可以#pragma warning(disable:4098),如果用VC,也可以在编译选项中直接Ignore有冲突的库

我就是这样做的但提示如下:
..\MyAppDlg.cpp(183) : error C2065: 'SetPubint' : undeclared identifier
..\MyAppDlg.cpp(184) : error C2065: 'GetPubint' : undeclared identifier
Error executing cl.exe.

MyApp.exe - 2 error(s), 0 warning(s)
greenabc 2004-11-10
  • 打赏
  • 举报
回复
非常感谢上面两位,不过问题还没解决:

In my app:
void __declspec(dllimport) SetPubint(iage);
m_LibClass.SetAge(int __declspec(dllimport) GetPubint() +10);
In the Static library:
int __declspec(dllexport) m_pubint;
int __declspec(dllexport) GetPubint()
{
return m_pubint;
}
void __declspec(dllexport) SetPubint(int pubint)
{
m_pubint=pubint;
}
In the debug output box:

..\MyAppDlg.cpp(183) : error C2182: 'SetPubint' : illegal use of type 'void'
..\MyAppDlg.cpp(183) : error C2491: 'SetPubint' : definition of dllimport data not allowed
..\MyAppDlg.cpp(183) : error C2205: 'SetPubint' : cannot initialize extern variables with block scope
..\MyAppDlg.cpp(184) : error C2144: syntax error : missing ')' before type 'int'
..\MyAppDlg.cpp(184) : error C2660: 'SetAge' : function does not take 0 parameters
..\MyAppDlg.cpp(184) : error C2059: syntax error : ')'
Error executing cl.exe.

MyApp.exe - 6 error(s), 0 warning(s)
puregrid 2004-11-09
  • 打赏
  • 举报
回复
1:表态库里的函数是全局的,只要在应用中包含相应声明的头文件,直接调用就行了;
2:可以#pragma warning(disable:4098),如果用VC,也可以在编译选项中直接Ignore有冲突的库
zhujianping_es 2004-11-09
  • 打赏
  • 举报
回复
Add the __declspec(dllexport) when declare in static library.
When use in your app,__declspec(dllimport) to declare them.

As to disable the warnings:
#pragma warning(disable:4098)
:)

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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