GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Apr-2009, 09:30
tandu tandu is offline
New Member
 
Join Date: Apr 2009
Location: Rome
Posts: 3
tandu is on a distinguished road

Linking problem with jpeg and the stdc++ (linux 64bits)


Hello,
This is my first post. My name is Giuseppe, I’m a programmer and work in image processing for satellite application.
I have a problem to compile a c++ code using jpeg lib on a 64 bits Linux machine (fedora 9 64bits).

To explain better the problem, I have the follows two simple program (in c and in c++)

CPP / C++ / C Code:
/*
*  jpegtest.c
*/ 

#include <stdio.h>
#include <jpeglib.h>

int main(int argc, char *argv[])
{
        struct jpeg_compress_struct cinfo;
        jpeg_create_compress(&cinfo);

        printf("hello world\n");
}

and

CPP / C++ / C Code:
/*
*  jpegtest.cpp
*/ 
#include <iostream>
#include <jpeglib.h>

using namespace std;

int main(int argc, char *argv[])
{
        struct jpeg_compress_struct cinfo;
        jpeg_create_compress(&cinfo);

        cout << "hello world" << endl;
}

If I try to compile, I obtain the follow situation:

gcc jpegtest.c -o jpegtest_c -ljpeg
OK
g++ jpegtest.cpp -o jpegtest_cpp –ljpeg
undefined reference to `jpeg_CreateCompress(jpeg_compress_struct*, int, unsigned long)'

gcc jpegtest.cpp -o jpegtest_cc -ljpeg -lstdc++
undefined reference to `jpeg_CreateCompress(jpeg_compress_struct*, int, unsigned long)'

I reach to compile c code that use jpeg lib but not c++ code!

its seams that the stdc++ make some confusion in the linking phase. I have already compiled that code on a 32bits linux machine without problems.

Someone can help me? Many thanks!

Giuseppe
  #2  
Old 08-Apr-2009, 10:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linking problem with jpeg and the stdc++ (linux 64bits)


Quote:
Originally Posted by tandu
Code:
g++ jpegtest.cpp -o jpegtest_cpp –ljpeg undefined reference to `jpeg_CreateCompress(jpeg_compress_struct*, int, unsigned long)'

What happens when you try
Code:
g++ jpegtest.cpp -o jpegtest_cpp -ljpeg

Note that my command line has an ascii '-' in front of the "ljpeg." Yours has something else. (Hex e2 80 93) Maybe your compiler is set up differently than on my installation, and I have no way of testing unicode or whatever other input encoding you (and your Fedora installation) are using. Note also, that my compiler gives a "no such file or directory message rather than the undefined symbol one, so I'm guessing that is not hte problem. Just a guess.

However...

The command line that I showed works on my Centos 5.2 64-bit workstation (g++ version 4.1.2). I have no way of testing Fedora 64-bit distributions these days, but I have noted in the past that sometimes some distributions (not just Fedora) don't set up library search paths correctly in cases where 32-bit and 64-bit libraries are both installed. So, if there is libjpeg.so in /usr/lib and also in /usr/lib64, the linker might erroneously pick the 32-bit library (in /usr/lib), in which case you could get an "undefined reference" error.


Quote:
Originally Posted by tandu
Code:
gcc jpegtest.cpp -o jpegtest_cc -ljpeg -lstdc++ undefined reference to `jpeg_CreateCompress(jpeg_compress_struct*, int, unsigned long)'

I'm not sure why you would want to do this, but I'm thinking that if the "g++" command line doesn't work, then "gcc...-lstdc++" won't work either.

Quote:
Originally Posted by tandu
its seams that the stdc++ make some confusion in the linking phase. I have already compiled that code on a 32bits linux machine without problems.

If my command line doesn't work, then try the following:

Look for libjpeg files in /usr/lib and /usr/lib64:

Code:
cd /usr/lib ll -d *jpeg* cd /usr/lib64 ll -d *jpeg*

If libjpeg.so appears in both directories, then you might try the following:
Code:
g++ jpegtest.cpp -L/usr/lib64 -ljpeg -ojpegtest_cpp

If this works, then you might even temporarily change the name of "/usr/lib/libjpeg.so" to something else. See Footnote.
Try compiling again with

Code:
g++ jpegtest.cpp -ljpeg -ojpegtest_cpp


Regards,

Dave

Footnote:

The "correct" thing to do, instead of renaming the library file of an installed 32-bit package is to use yum to uninstall the 32-bit package. I can't imagine that you would need the 32-bit library on a 64-bit installation anyhow. The "rename" trick is just for verification.
  #3  
Old 08-Apr-2009, 10:51
tandu tandu is offline
New Member
 
Join Date: Apr 2009
Location: Rome
Posts: 3
tandu is on a distinguished road

Re: Linking problem with jpeg and the stdc++ (linux 64bits)


Hello Dave,
Thanks a lot for the answer!

Now in the machine exists only the jpeg lib installed from the source distribution jpeg-6b.tar.gz, libjpeg appears only in the directory /usr/local/lib (checked with ldconfig -v).
I have changed the order of the argument in the command line (as you suggest) but the result is the same.


It’s very strange problem, I don’t understand why the compilation ends successful when compile c code end fails with c++ code ….. maybe this is a sign to change work?!
  #4  
Old 08-Apr-2009, 11:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linking problem with jpeg and the stdc++ (linux 64bits)


Quote:
Originally Posted by tandu
...installed from the source distribution jpeg-6b.tar.gz, libjpeg appears only in the directory /usr/local/lib (checked with ldconfig -v)
.
How did you install the library? Did you compile it from scratch or what? I always use yum for such things so that all file locations are known and so that upgrades can happen in a sane way.

The fact that you didn't get a messag about not finding the file indicates that it found a libjpeg file somewhere. See Footnote.

Try
Code:
yum info "*libjpeg*"

You might try uninstalling the jpeg stuff that you compiled from scratch and, then:

As root:
Code:
yum install libjpeg libjpeg-devel

This works with RedHat-like distributions like Centos, and I assume that it would work with Fedora.

Sorry that I can't help beyond this point.

Regards,

Dave


Footnote:

In your source installation does /usr/include/jpeglib.h have the following near the beginning:
CPP / C++ / C Code:
#ifdef __cplusplus
extern "C" {
#endif

And the following near the end:
CPP / C++ / C Code:
#ifdef __cplusplus
}
#endif

If not, then including this header in a c++ program won't work. You could get the undefined symbol error message.
  #5  
Old 08-Apr-2009, 15:26
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linking problem with jpeg and the stdc++ (linux 64bits)


Quote:
Originally Posted by davekw7x
...
In your source installation does /usr/include/jpeglib.h have the following near the beginning:
CPP / C++ / C Code:
#ifdef __cplusplus
extern "C" {
#endif

And the following near the end:
CPP / C++ / C Code:
#ifdef __cplusplus
}
#endif
...

I just downloaded and untarred jpegsrc.v6b.tar.gz.

Indeed, the cplusplus stuff was not in jpeglib.h.

Here's a plan: Copy jpeglib.h to a file with another name (just in case). For example:
Code:
cp jpeglib.h jpeglib_distribution.h

Then edit jpeglib.h to insert the lines shown above something like this:

Change stuff at the beginning (somewhere around line 13) to:
CPP / C++ / C Code:
#ifndef JPEGLIB_H
#define JPEGLIB_H

#ifdef __cplusplus
extern "C" {
#endif

/*
 * First we include the configuration files that record how this
 * installation of the JPEG library is set up.  jconfig.h can be
.
.
.

And near the end:
CPP / C++ / C Code:
#ifdef JPEG_INTERNALS
#include "jpegint.h"		/* fetch private declarations */
#include "jerror.h"		/* fetch error codes too */
#endif

#ifdef __cplusplus
}
#endif

#endif /* JPEGLIB_H */

Execute the following from the jpeg-6b directory:

Code:
make distclean ./configure make

If all goes well, then, as root:
Code:
make install


Regards,

Dave

Footnote: I still recommend using yum to manage packages like this, but it is possible, I think, to do it from source.
  #6  
Old 09-Apr-2009, 03:15
tandu tandu is offline
New Member
 
Join Date: Apr 2009
Location: Rome
Posts: 3
tandu is on a distinguished road

Re: Linking problem with jpeg and the stdc++ (linux 64bits)


Works!!!

I'm very happy, thanks a lot Dave! I'll keep in mind your suggestions about the use of yum to manage packages!!

Giuseppe
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:18.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.