Linking errors on Linux gcc 4.3.4: "undefined reference" related to "jpeg" library (e.g., undefined reference to `jpeg_std_error(jpeg_error_mgr*)')

13,047

Following up on the comments: are you sure you installed libjpeg? Run

find $HOME -iname libjpeg\*so\*

to find the libjpeg you need to link to. This command will most likely yield several results.

You can simply take the first line of this output and append it to your compilation command, but leave out the "-ljpeg" suggested above.

If it does not give any output at all, you don't have libjpeg installed in your home. So either

  • install libjpeg

  • disable the part of the code that needs it if you don't need it either

  • extend your search,

like

find /usr /opt -iname libjpeg\*so\*

... and try with one of the libjpeg installations on the system.

EDIT (after libjpeg was found on the system):

Try appending

/usr/lib/libjpeg.so.62

to the compile command (the OPTIONS variable in your Makefile). If that doesn't work, try

-L/usr/lib -ljpeg

(Edit by Mats:)

Also make sure EVERY place where #include "jpeglib.h" is covered by extern "C" { ... }. It may make sense to replace these with a c++ wrapper include, which does the addition of extern "C" { ... } in one place, and then just include "wrapped_jpeglib.h" in the relevant places.

(End Edit by Mats)

Share:
13,047
Ant
Author by

Ant

Updated on June 05, 2022

Comments

  • Ant
    Ant almost 2 years

    I am working with a large C++ code that has been developed over a few years. I have added to the code and have been successfully running it on Mac OX 10.7.5. However, it is very slow and I now want to run it on a cluster (g++ (SUSE Linux) 4.3.4 [gcc-4_3-branch revision 152973]). I am not very experienced as far as C++ and I'm a complete newbie as far as dealing with compilation / linking.

    I am unable to get past a linking error related to a "jpeg" library and would be very grateful for any help. I have the library code installed in a directory where my code resides, but my understanding is that it is also available from the compiler.

    There is a lot of error output, so I won't post it all. Here is the first part:

    /data/place/number/account/program/libraries/libfile_intel.a(Grid.o): In function program::Grid<double>::SaveToJPG(char const*, int, bool, bool) const': Grid.cpp:(.text._ZNK3program20GridIdE9SaveToJPGEPKcibb[program::Grid<double>::SaveToJPG(char const*, int, bool, bool) const]+0x499): undefined reference to jpeg_std_error(jpeg_error_mgr*)' Grid.cpp:(.text._ZNK3program20GridIdE9SaveToJPGEPKcibb[program::Grid::SaveToJPG(char const*, int, bool, bool) const]+0x4b5): undefined reference to `jpeg_CreateCompress(jpeg_compress_struct*, int, unsigned long)'

    What I have tried:

    • When I did a search in Google on this problem, I discovered from an old forum post that the jpeg library compiles in C. This apparently causes the C++ compiler to mess up names. I'm not clear on what this means, but I followed advice to add:

    extern "C" {
    #include "jpeglib.h" }

    in all of the files in my jpeg directory that contain "#include "jpeglib.h." I wrote, "jpeglib.h" because, as I mentioned, I have a directory containing the jpeg code. However, I did also try to use the jpeg code provided by default and I got a message saying it is not available on the cluster compiler.

    • In my makefile, I have checked all of my includes and paths. These do not seem to be the problem. Also, I have similar paths and includes for other libraries in other directories, and they're fine.

    • I also added: -lstdc++ to my list of compiler options (from advice on another old forum post). It had zero effect.

    One weird observation I have made is that when I do "make" of the entire code (several libraries within various directories), the jpeg library does not make unless I specifically go into its directory and do "make" there. As I said, the code is way too big to post this to demonstrate that I have no path errors. I am pretty sure I don't, but this behavior seems very odd.

    If anyone is still with me after this long post, I would be very grateful for any tips. Thanks.

    Edit: In my makefile, here is what i have for compiling/linking:

    CC = g++

    OPTIONS = -O3 -fpermissive -w -DSAMG_UNIX_LINUX -DSAMG_LCASE_USCORE -DNDEBUG -DCSP_WITH_SAMG_SOLVER

    Edit 2: result of Svens's advice

    I did:

    find /usr /opt -iname libjpeg*so*

    And got:

    /usr/lib/libjpeg.so.62

    /usr/lib/libjpeg.so.62.0.0

    /usr/lib64/libjpeg.so.62

    /usr/lib64/libjpeg.so.62.0.0

    /usr/lib64/libjpeg.so find:

    /usr/lib64/mozilla': Permission denied find: /usr/lpp/mmfs/gui/runtime': Permission denied me@login1:/data/place/number/account/program/support_libraries/jpeg> cd ../../libraries/ me@login1:/data/place/number/account/program/libraries> find $HOME -iname libjpeg*so*

    If my understanding is correct, the library exists and has been found in the "libraries" directory, which is where I expect. My main makefile that links all the libraries has an include path to the directory where the jpeg library resides. The other libraries in that directory are found. What is the "permission denied" doing?

    Edit 3: result of Sven's advice to change the makefile options:

    Here's what I changed:

    • First added "-lgems3k -L/usr/lib -ljpeg" to my LIBS (the libraries). This leads to the errors below.
    • Then added (leads to same error as original): OPTIONS = -O3 -fpermissive -w -DSAMG_UNIX_LINUX -DSAMG_LCASE_USCORE -DNDEBUG -DCSP_WITH_SAMG_SOLVER -/usr/lib/libjpeg.so.62

    -L/usr/lib -ljpeg /usr/lib64/gcc/x86_64-suse-linux/4.3/../../../../x86_64-suse-linux/bin/ld:

    skipping incompatible /usr/lib/libm.so when searching for -lm

    /usr/lib64/gcc/x86_64-suse-linux/4.3/../../../../x86_64-suse-linux/bin/ld:

    skipping incompatible /usr/lib/libm.a when searching for -lm

    /usr/lib64/gcc/x86_64-suse-linux/4.3/../../../../x86_64-suse-linux/bin/ld:

    skipping incompatible /usr/lib/libc.so when searching for -lc

    /usr/lib64/gcc/x86_64-suse-linux/4.3/../../../../x86_64-suse-linux/bin/ld:

    skipping incompatible /usr/lib/libc.a when searching for -lc

    /data/place/number/account/program/libraries/lib1_intel.a(Grid.o): In

    function `program::Grid::SaveToJPG(char const*, int, bool, bool) const':

    Grid.cpp:(.text._ZNK3program20GridIdE9SaveToJPGEPKcibb[program::Grid::SaveToJPG(char const*, int, bool, bool) const]+0x499): undefined reference to `jpeg_std_error(jpeg_error_mgr*)'

    and so on...

  • Ant
    Ant almost 11 years
    Hi Sven. Thanks for your help. I apologize in advance because I'm not sure I understand you advice... I did "find $HOME..." and got nothing. So then I did the wider search and did get output. I'll post it above in my question as an edit.
  • Sven
    Sven almost 11 years
    That means you don't have libjpeg installed. In your post, you write program::Grid<double>::SaveToJPG(...) Do you need this function for your special use? If you don't, edit the source code and comment it out, then recompile. It's not an elegant solution, but one that might work. If you really do need libjpeg, you'll have to install it.
  • Ant
    Ant almost 11 years
    Sven, sorry to be an idiot, but what do you mean by "not installed?" I see it in my libraries folder. Do you mean, not linked? Also, when you say, "write program::Grid<double>::SaveToJPG," do you want me to add that command somewhere? Answer to your update: no, I don't think I even use jpeg.
  • Sven
    Sven almost 11 years
    Sorry, I overlooked your edit. Try to append "/usr/lib/libjpeg.so.62" to your compile command. What's the error message now? (You can safely ignore the "permission denied" stuff.)
  • Ant
    Ant almost 11 years
    Aha, I see what you're saying. You're telling me to simply comment out the problem functions. I'll give it a shot (it will take a while because there are many). If you would have time to explain what you mean by "not installed" when I see that a jpeg library exists in /libraries, I would be very grateful! Thank you so much. ****Edit: I'll first append that update to my compile command before commenting out any functions.***Edit: Sven, this might take a wee while because I have to figure out where to put that appendix!
  • Sven
    Sven almost 11 years
    Use the OPTIONS variable in your Makefile. I edited my answer to include this.
  • Ant
    Ant almost 11 years
    OK, thanks a lot for that clarification. It may take about 20 - 30 minutes to compile.
  • Sven
    Sven almost 11 years
    Try "make -jNUMCPUS". Choose NUMCPUS roughly equal to twice the number of CPU cores on the system. That should speed up things. (If this fails in a different way than described above, the application you compile does not like parallel "make" invocation, and you're back to waiting 30 minutes.)
  • Ant
    Ant almost 11 years
    Thanks Sven. Actually, I realized that I don't need to compile all of the libraries, just the jpeg one. I'll post the output above in the question as an edit.
  • Sven
    Sven almost 11 years