我正在修改一个开源代码(pngcrush)并希望包含一些c ++头文件(fstream,iostream)
抱怨没有这样的文件.
我如何包含它们?
谢谢
-编辑-
adler32.c:60: error: ‘uLong adler32’ redeclared as different kind of symbol
zlib.h:1469: error: previous declaration of ‘uLong adler32(uLong, const Bytef*, uInt)’
adler32.c:60: error: ‘adler’ was not declared in this scope
adler32.c:60: error: ‘buf’ was not declared in this scope
adler32.c:60: error: ‘len’ was not declared in this scope
adler32.c:64: error: expected unqualified-id before ‘{’ token
adler32.c:12: warning: ‘uLong adler32_combine_(uLong, uLong, long int)’ declared ‘static’ but never defined
make: *** [adler32.o] Error 1
Compilation exited abnormally with code 2 at Wed Jan 5 18:51:02
Run Code Online (Sandbox Code Playgroud)
在代码
uLong ZEXPORT adler32(adler, buf, len)
uLong adler;
const Bytef *buf;
uInt len;
{
unsigned long sum2;
unsigned n;
Run Code Online (Sandbox Code Playgroud)
我结束了它编译为C代码(使用gcc),但#ifdef来__cplusplus为extern"C" {} #ENDIF ..我把那的#ifdef以下项目的其他C代码.
它现在编译,但有一个警告(ranlib没有符号,我googled它,看起来它只发生在mac os)
ar rcs libpngcrush.a png.o pngerror.o pngget.o pngmem.o pngpread.o pngread.o pngrio.o pngrtran.o pngrutil.o pngset.o pngtrans.o pngwio.o pngwrite.o pngwtran.o pngwutil.o adler32.o compress.o crc32.o deflate.o infback.o inffast.o inflate.o inftrees.o trees.o uncompr.o zutil.o simplepngcrush.o
/usr/bin/ranlib: file: libpngcrush.a(pngpread.o) has no symbols
Run Code Online (Sandbox Code Playgroud)
另外,当我将这个libpngcrush.a链接到我的c ++代码时,
它会在我第一次调用库中的函数时死掉.
Run till exit from #0 0x0000000100089071 in simplepngcrush () at tokenlist.h:118
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x00000001000890f8 in simplepngcrush () at tokenlist.h:118
Run Code Online (Sandbox Code Playgroud)
看起来,我甚至无法到达被调用函数的第一行.
你不能.C++源代码无法从C中使用.
唯一的方法是切换到C++:用C++转换你的C代码(这通常不需要或很少修改代码,只需要构建脚本以便切换编译器)然后使用C++头和工具.
编辑:
C++使用与C不同的方式来识别符号(函数,变量和东西).
uLong ZEXPORT adler32(adler, buf, len)只是adler32由C 调用,而它可以_Z7adler32lPKci在C++中调用.这是为了提供像重载这样的东西.
如果你想让编译器给它提供与C给出的相同的名称,你可以将声明放在extern "C":
extern "C"
{
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len);
}
Run Code Online (Sandbox Code Playgroud)