Include C ++ (fstream) header from c based code?

I am changing open source (pngcrush) and want to include some C ++ header (fstream, iostream)

Make complaints that there are no such files.
How to enable them?

thank

-Edit -

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

in code

uLong ZEXPORT adler32(adler, buf, len)
    uLong adler;
    const Bytef *buf;
    uInt len;
{   
    unsigned long sum2;
    unsigned n;
  • second edit -

I ended up compiling it as c-code (using gcc), but C # ifdef __cplusplus extern "C" {} #endif .. I put this #ifdef following other project code codes.

Now it compiles, but with a warning (ranlib has no characters, I googled, it looks like this only happens on 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

also, when I bind this libpngcrush.a with my C ++ code,
it dies when I call the function that is in the library for the first time.

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

It looks like I can't even get to the first line of the called function.

+3
4

. ++ C.

++: C- ++ ( , ), ++.


EDIT:

++ , C (, ..).

uLong ZEXPORT adler32(adler, buf, len) adler32 C, _Z7adler32lPKci ++. , .

, , C, extern "C":

extern "C"
{
    uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len);
}
+5

. C, C FILE * printf ..

, ++ C : - ++, C - ++ C.

fstream C, iostream C, . , (, ).

, , ++ C. forward-declarations ( struct) free-functions, , , C .

, , ( ).

+3

C C, , , , , .., ++ C

, , C- ++ - . , C ++.

, C io C-; FILE, fopen, fread, fwrite, fprintf, stdout, stderr ..

+2

. C, ++ , . C ++ - .

Instead, move on to creating the entire object in C ++, which may well include minor changes to the code base, depending on how it is written.

(This is not a recommendation, this is the only way to use these headers. Instead, if it is written in C, stick with C. Then it will remain supported with the original code base.)

+1
source

Source: https://habr.com/ru/post/1783583/


All Articles