12

When linking a project I am working on, the linker gives the following errors:

/usr/bin/ld: ../Includes and Libs/lib/libsfml21rca.a(SoundFile.o): undefined reference to symbol 'sf_read_short@@libsndfile.so.1.0'

/usr/bin/ld: note: 'sf_read_short@@libsndfile.so.1.0' is defined in DSO /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libsndfile.so so try adding it to the linker command line

/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libsndfile.so: could not read symbols: Invalid operation

The thing is, libsndfile.so is already linked before libsfml21rca.a, so I have no idea where the problem is.

I'm using Code::Blocks 10.05

Thanks for help in advance

EDIT:

Here is the linking command:

g++ -L"Includes and Libs/lib" -L"Includes and Libs/lib/raknet3_731" -L"Includes and Libs/lib/d3d_new/x86" -L"Includes and Libs/lib/ogg" -L"Includes and Libs/lib/sdl" -LBullet/lib -o (filename) ...(a whole lot of object files) -lGLEW -lglfw -lGL -lGLU -lpthread -lopenal -ljpeg -lfreetype -lsndfile -lXrandr -lsfml-system -lsfml-window -lsfml-audio ../Bullet/lib/LinearMath.lib ../Bullet/lib/BulletCollision.lib ../Bullet/lib/BulletDynamics.lib "../Includes and Libs/lib/raknet3_731/RakNetLibStaticDebug.lib" "../Includes and Libs/lib/libsfml21rca.a" ../../../../../../home/msabol/Desktop/SFML/sfml2st/sfmlVideo/sfmlVideo/bin/Release/libsfmlVideo.a ../../../../../../home/msabol/Desktop/SFML/sfmlVideo/bin/Release/libsfmlVideo.a

matuzalem
  • 367
  • 1
  • 2
  • 14
  • 4
    Try changing the order of linking? – PP. Jul 19 '13 at 08:40
  • 1
    How exactly am I supposed to change it? Everything is linked in correct order, or at least it seems so. – matuzalem Jul 19 '13 at 08:43
  • Add the command C::B is using to link to your question. (do not add it as a comment!) – greatwolf Jul 19 '13 at 08:46
  • possible duplicate of [library is linked but reference is undefined](http://stackoverflow.com/questions/7783345/library-is-linked-but-reference-is-undefined) –  Jul 19 '13 at 09:35
  • @H2CO3 also, not duplicated intentionally, I just didn't realize the fault in my linking order. – matuzalem Jul 19 '13 at 10:34

1 Answers1

10

The linker only runs one pass over the library files. So if you have something in Library A that needs something in Library B, you need to have g++ objects... -llibA -llibB, if you use g++ objects... -llibB -llibA it will fail in the manner you show.

So, in your case, put the -lsndfile after "../Includes and Libs/lib/libsfml21rca.a".

(And whose idea was it to put spaces in a the "Includes and Libs" directory - not the best idea I've seen...)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 1
    I'll give it a shot. In my defence, I jumped on this project few days ago, so all the terrible names (there is more of it) were not made by me. **EDIT:** Blast damnation, it worked! Thank you, Mats Peterson. – matuzalem Jul 19 '13 at 09:01
  • @Mats frankly, I find the single pass over library more off-putting. There have been countless SO questions like the OP's all because of `ld` linker's limition. Microsoft's `LINK` tool doesn't have this problem for instance. Neither does Embarcadero's ilinker. – greatwolf Jul 19 '13 at 09:05
  • @greatwolf Unfortunately, my job is to make the project run on Unix platforms. So much for Microsoft's `LINK`. – matuzalem Jul 19 '13 at 09:08
  • It is open source, I'm sure a patch would be welcome... Although I suspect that would break things somewhere - or at least if it was really easy to fix without drawback, someone would have fixed it. – Mats Petersson Jul 19 '13 at 09:09