C与C++编译结果不一样

赤色彗星 2011-01-03 11:18:54
下面的程序是一个动态连接库,我开始是编译MyDll.c,结果出现下面的错误,把文件名改为MyDll.cpp就没有错误了,我知道这是C和C++的区别,可就是不知道哪里的区别,请高手们指导我一下,小弟感激不尽

正在编译...
1>MyDll.c
1>f:\windows编程\dll1\dll1\mydll.h(7) : error C2059: 语法错误 : “字符串”
1>f:\windows编程\dll1\dll1\mydll.h(8) : error C2059: 语法错误 : “字符串”
1>生成日志保存在“file://f:\Windows编程\dll1\dll1\Debug\BuildLog.htm”
1>dll1 - 2 个错误,0 个警告

下面是源文件

MyDll.h


#include <windows.h>
#define MYDLLAPI extern "C" _declspec(dllexport)
#include "MyDll.h"

int g_nResult;

int Add(int nLeft,int nRight)
{
g_nResult = nLeft + nRight;
return(g_nResult);
}



MyDll.c


#include <windows.h>
#define MYDLLAPI extern "C" _declspec(dllexport)
#include "MyDll.h"

int g_nResult;

int Add(int nLeft,int nRight)
{
g_nResult = nLeft + nRight;
return(g_nResult);
}
...全文
234 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
拓跋野 2011-01-06
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 shineofgod 的回复:]

然后.cpp这样写:C/C++ code

#include<Windows.h>
#define MYLIBAPI extern "C" __declspec(dllexport)
#include"mylib.h"
int add(int nleft,int nright){
return nleft+nright;
}
[/Quote]
不懂得地方最好自己动手试试,像我学这一节的时候,就有objdump对dll分析过,正如书上说的那样,c与c++在导出函数时函数名是不同的。
赤色彗星 2011-01-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xunxun1982 的回复:]
头文件删除#include "MyDll.h"
int Add(int nLeft,int nRight)有2个实现,删除一个
[/Quote]

我只是在头文件声明一下函数头啊,再说为什么C++能编译通过呢?
xunxun 2011-01-03
  • 打赏
  • 举报
回复
头文件删除#include "MyDll.h"
int Add(int nLeft,int nRight)有2个实现,删除一个
赤色彗星 2011-01-03
  • 打赏
  • 举报
回复
哪位大侠帮帮我啊
赤色彗星 2011-01-03
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 renxu350 的回复:]
extern "C" 表示用 C++ 的方式导出文件,
你把 extern "C" 去掉试一试用 *.c 的扩展名编译,,,,,
[/Quote]

那12楼的怎么没问题啊?????
赤色彗星 2011-01-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xunxun1982 的回复:]
不论是c还是cpp,gcc都编译不通过
[/Quote]

我用的是VS2005
赤色彗星 2011-01-03
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 shineofgod 的回复:]
lz是不是在学windows核心编程啊,这个不就是里面的例子吗?不过你写的不对,你的头文件没有起到申明的作用。
.h这样写才对

C/C++ code

#ifndef MYLIB
#define MYLIB
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif
MYLI……
[/Quote]
对啊,那本书有些知识不太理解,不知道把整本书全看懂会到什么水平。。。。。。。。。。。
拓跋野 2011-01-03
  • 打赏
  • 举报
回复
然后.cpp这样写:

#include<Windows.h>
#define MYLIBAPI extern "C" __declspec(dllexport)
#include"mylib.h"
int add(int nleft,int nright){
return nleft+nright;
}

拓跋野 2011-01-03
  • 打赏
  • 举报
回复
lz是不是在学windows核心编程啊,这个不就是里面的例子吗?不过你写的不对,你的头文件没有起到申明的作用。
.h这样写才对

#ifndef MYLIB
#define MYLIB
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif
MYLIBAPI int add(int nleft,int nright);
#endif
renxu350 2011-01-03
  • 打赏
  • 举报
回复
C、C++两种方式导出的DLL,其函数名称是不一样的,,,,,
renxu350 2011-01-03
  • 打赏
  • 举报
回复
extern "C" 表示用 C++ 的方式导出文件,
你把 extern "C" 去掉试一试用 *.c 的扩展名编译,,,,,
無_1024 2011-01-03
  • 打赏
  • 举报
回复
预编译下看看
乐CC 2011-01-03
  • 打赏
  • 举报
回复
头文件定义有问题
maoxing63570 2011-01-03
  • 打赏
  • 举报
回复

MyDll.h

#ifndef MyDLLAPI
#include <windows.h>//没用到API,你包含它干什么
#define MYDLLAPI extern "C" _declspec(dllimport)对于DLL来说,头文件是给使用这个dll文件的人看的,也是给使用这个dll的人用的,所以应该用导入,当然你可不这么做
#include "MyDll.h"//你自己包含自己干什么
#endif
int g_nResult;

int Add(int nLeft,int nRight);这里只需要声明,不需要定义,不要忘记头文件的作用



#include <windows.h>//这里同样不需要这个头文件
#define MYDLLAPI extern "C" _declspec(dllexport)
#include "MyDll.h"

int g_nResult;//在头文件中有相关的声明了,在这里不需要了

int Add(int nLeft,int nRight)
{
g_nResult = nLeft + nRight;
return(g_nResult);
}
AAA20090987 2011-01-03
  • 打赏
  • 举报
回复
#ifndef MYDLL_H_
#define MYDLL_H_

#include <windows.h>
#define MYDLLAPI extern "C" _declspec(dllexport)
#include "MyDll.h"

int g_nResult;

int Add(int nLeft,int nRight)
{
g_nResult = nLeft + nRight;
return(g_nResult);
}

#endif


加上几句
xunxun 2011-01-03
  • 打赏
  • 举报
回复
不论是c还是cpp,gcc都编译不通过
赤色彗星 2011-01-03
  • 打赏
  • 举报
回复
有高手没??

69,336

社区成员

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

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