Linking to a C library in a C++ program

Discussion about everything. New games, 3d math, development tips...
Post Reply
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Linking to a C library in a C++ program

Post by JP »

I'm trying to link to a C library (libjpeg, not using irrlicht) from my C++ program but it won't link properly and brings up errors saying functions are not declared wheras it links fine if i'm linking to it from a C program.

I've seen some usage of 'extern "C"' as a wrapper around code for linking between the two languages but i couldn't get this to work either.

Any ideas?
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

jpeglib.h is indeed missing extern "C", but you should be able to simply wrap the include like this:

Code: Select all

extern "C"
{
#include <jpeglib.h>
}
Is that not working for you?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

No i initially tried that and it didn't work and then i tried putting it in jpeglib.h as well and that didn't help either.

Basically without extern "C" the linking errors i get are function_name(parameters) 'not found' but with the extern "C" added in it loses the parameters from the error and it's just the function name it complains about.

I'm using Visual Studio, is there possibly some compiler/project setting that might affect this?
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Reaaally stupid question, but are you actually linking against the libjpeg library? I mean, in the configuration that you're building? I have a bad habit of adding libraries to the debug build and forgetting to add them to the release build. ;)
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

How dare you suggest i'd be so foolish!

*checks*

nope, all linked up properly as far as i can tell :lol:

Here's what happens without extern "C":
PS3_PPU_Release/JPEG.obj: In function `.Decompress_JPEG(jpeg_decompress_struct*, tImageJPG*)':
JPEG.cpp:(.text+0x24): undefined reference to `.jpeg_read_header(jpeg_decompress_struct*, unsigned char)'
....etc....
and with extern "C":
PS3_PPU_Release/JPEG.obj: In function `.Decompress_JPEG(jpeg_decompress_struct*, tImageJPG*)':
JPEG.cpp:(.text+0x24): undefined reference to `.jpeg_read_header'
....etc....

I even made sure that JPEG.cpp was indeed a C++ file and not a C file as that can cause problems like this.
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Sorry, this Works For Me, using a libjpeg library swiped from ClanLib.

Code: Select all

#include <stdio.h> // Required by jpeglib.h

extern "C"
{
#include <C:/dev/jpeg-6b-msvc80/include/jpeglib.h>
}

#pragma comment(lib, "C:/dev/jpeg-6b-msvc80/lib/libjpeg-static-mt.lib")
#pragma comment(linker, "/NODEFAULTLIB:MSVCRTD.LIB")

int main(int argc, char* argv[])
{
	struct jpeg_decompress_struct decompressStruct;
	int result = jpeg_read_header(&decompressStruct, false);
	return result;
}
I think you need to sacrifice a chicken, possibly even a goat.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Well i've snagged that lib aswell and i'll try it out with that one tomorrow. I had to build the lib myself so i could have made an error there (though it does work with C programs).
Image Image Image
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

No joy with that lib version either :(

Also tried out the pragma linking instead of in the project options but the compiler just boldly states that it's chosen to ignore the pragma comment...
Image Image Image
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I'm gonna say that you can all stop worrying about this problem now and get a good nights sleep instead of lying awake fretting about me.

Turns out the PS3 SDK has jpg decoding support in it so i'm going to investigate this instead!
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

I NEED CLOSURE ON THIS ANECDOTE!
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
FlyingIsFun1217
Posts: 219
Joined: Fri Apr 13, 2007 8:29 pm
Location: Illinois
Contact:

Post by FlyingIsFun1217 »

If I remember my reading right, just append the letter c in front of the included header file. For example:
#include <cmath.h>
I've never run into this problem though, so I can't say whether or not it would work. Might also depend on your compiler ;)

FlyingIsFun1217
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

I think that only works for the standard c headers, such as math.h, as those are actually defined as cmath.h as well? I can't imagine changing the actual name of the header file wouldn't confuse the compiler.

As it turns out the PS3 implementation loads 16 jpg textures in 6.4 seconds whereas libjpeg did it in 8.4 seconds, so now i have a simpler method which is actually faster (due to the fact that it uses the SPUs, multi processors).
Image Image Image
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

<cmath> would be the correct way to include the math headers in C++. These are the versions which may take some advantage from being used only by C++ compilers such that templates and stuff can go into those headers. But external library headers are just as they are.
But Irrlicht also uses libjpeg and does just as rogerborg said. So for all those machines we are currently compiling on it works.
Post Reply