gcc undefined reference to
Asked Answered
E

2

10

I'm running..

gcc -c -I/usr/vt/sample ttssample.c
gcc -L. -lttsapi ttssample.o -o ttsample

and I'm getting the following error...

ttssample.o: In function `_TTSFile':
ttssample.c:(.text+0x352): undefined reference to `TTSRequestFile'
ttssample.o: In function `_TTSFileEx':
ttssample.c:(.text+0x5e0): undefined reference to `TTSRequestFileEx'
ttssample.o: In function `_TTSBuffer':
ttssample.c:(.text+0x833): undefined reference to `_TTSRequestBuffer'
ttssample.o: In function `_TTSBufferEx':
ttssample.c:(.text+0xabd): undefined reference to `_TTSRequestBufferEx'
ttssample.o: In function `_TTSBuffering_cont':
ttssample.c:(.text+0xcbf): undefined reference to `_TTSRequestBuffer'
ttssample.o: In function `_TTSBuffering_stop':
ttssample.c:(.text+0xf2d): undefined reference to `_TTSRequestBuffer'
ttssample.o: In function `_TTSBuffering_SSML':
ttssample.c:(.text+0x122b): undefined reference to `_TTSRequestBufferSSMLEx'   
ttssample.o: In function `_TTSStatus':
ttssample.c:(.text+0x157b): undefined reference to `TTSRequestStatus'
collect2: ld returned 1 exit status

and TTSRequestFile is in the lib header but it has DllExport on the front of it which I'm wondering is the cause of my error? Any help much appreciated.

DllExport int TTSRequestFile(char *szServer, int nPort, char *pText, int nTextLen, char *szSaveDir, char *szSaveFile, int nSpeakerID, int nVoiceFormat);
Eos answered 26/12, 2012 at 14:41 Comment(0)
A
24

Your link command is wrong. Libraries should be specified at the end of the command:

gcc ttssample.o -o ttsample -L. -lttsapi
Abernon answered 26/12, 2012 at 15:18 Comment(2)
Any idea as to why this would error running the executable? error while loading shared libraries: -melf_x86_64: cannot open shared object file: No such file or directoryEos
@Eos Because the library is not in a directory searched by default by the runtime loader. You would need to put the library in a searched directory (like /usr/local/lib), or export the LD_LIBRARY_PATH environment variable to include the directory where the library resides (like LD_LIBRARY_PATH="/home/me/mylib"), or you would link your program using an rpath that points to the directory where the lib resides, or use the $ORIGIN feature of rpath to always look for the lib in a directory with a relative path to that of the executable. Search Google for "rpath origin" to find more about it.Abernon
O
-1

You could add preprocessor ifdefines around the DllExport call like so:

#ifdef _WIN32
// we are on windows

#elif defined __linux__
//we are on linux

#elif defined __APPLE__&__MACH__
// we are on mac

#endif // os specific

I added for the three platforms I have been compiling cross platform for. Do note that the keywords I use to recognize platforms may change, but the _WIN32 one has been tested with windows 7 and 8. I found these a year ago on sourceforge I think. I couldn't find the page right now, but I'll get back to you if I find it.

As I cannot yet comment on Nikos C's answer I'll comment it here: Your link command is correct, I can of course not see you files, so I assume your paths are correct. What is important is that -l's need to be in correct order according to dependencies, but this is usually not a problem as far as I have experienced.

Obsequies answered 26/12, 2012 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.