Linking to a C library in a C++ program
Linking to a C library in a C++ program
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?
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?
-
rogerborg
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
jpeglib.h is indeed missing extern "C", but you should be able to simply wrap the include like this:
Is that not working for you?
Code: Select all
extern "C"
{
#include <jpeglib.h>
}
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
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?
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?
-
rogerborg
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
How dare you suggest i'd be so foolish!
*checks*
nope, all linked up properly as far as i can tell
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.
*checks*
nope, all linked up properly as far as i can tell
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.
-
rogerborg
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
Sorry, this Works For Me, using a libjpeg library swiped from ClanLib.
I think you need to sacrifice a chicken, possibly even a goat.
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;
}
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
-
rogerborg
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
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
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:
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).
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).
-
hybrid
- Admin
- Posts: 14143
- Joined: Wed Apr 19, 2006 9:20 pm
- Location: Oldenburg(Oldb), Germany
- Contact:
<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.
But Irrlicht also uses libjpeg and does just as rogerborg said. So for all those machines we are currently compiling on it works.
