Ubuntu libdispatch - c

I am trying to port a program that uses GCD (Grand Central Dispatch) from OSX to Ubuntu 11.10. I installed libdispatch but I keep getting the following error:
undefined reference to dispatch_main()
The strange thing is that dispatch_main() is declared in a header file that I include and I call other functions declared in that header file and the compiler recognizes them. It is only dispatch_main() that it cannot see and if I call dispatch_main(2) it says that there are too many arguments, so I know the compiler can see the header.
I tried separating the compile and link steps (clang -c...) since that worked for an undefined reference error before, but it doesn't seem to do anything here...
Anybody have any suggestions? I'm pretty stumped on this one...

It sounds like you are missing the library from your link line. When you compile your program into an executable, add the library to the command. I am guessing it should look something like this:
clang x.c y.c z.c -ldispatch

Related

Linking shared libraries with gcc on Linux

I need to compile and, most importantly, link a C program that uses a proprietary function present in a shared library file. Because of lack of communication with the previous development team, there is no proper documentation. I declared a function prototype (because I know the number and type of arguments):
int CustomFunction(unsigned char *in, int size);
Since that function name can be grepped from /customlibs/libcustom.so, I tried to compile the code and link it like this:
gcc -L/customlibs testing.c -o testing -lcustom
Which throws a few error messages looking like this:
/customlibs/libcustom.so: undefined reference to `AnotherCustomFunction'
Obviously, I need to tell linker to include other libraries as well, and, to make things worse, they need to be in certain order. I tried exporting LD_LIBRARY_PATH, using -Wl,-rpath=, -Wl,--no-undefined and -Wl,--start-group. Is there an easy way to give the linker all the .so files without the proper order?
I found the solution (or a workaround) to my problem: adding -Wl,--warn-unresolved-symbols, which turns errors to warnings. Note that this works only if you are ABSOLUTELY certain your function does not depend on the symbols mentioned in undefined refernce to: messages.
Add them on the command line is a way to do it. Something like this below. The LD_LIBRARY_PATH tells gcc where to look for libraries, but you still need to say what libraries to include.
gcc -L/customlibs testing.c -o testing -lcustom -lmylib1 -lmylib2 -lmylib3
You should also include all the header files of your shared library by adding the -I option of gcc, for example : gcc [...] -I/path/to/your/lib/header/files [...]

"Undefined reference" errors when linking in DMD

When I am compiling a program that requires the mondo library in D, I get slammed with a wall of errors from the linker. The last two lines, which are the only ones remotely legible are:
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
All other lines prior to it, however, essentially say the same thing: "undefined reference to ..."
I've done my research about this problem before asking on Stack Overflow, but what worked for everybody else does not seem to work for me. (Of course, I had to look up this problem with C or C++ builds, but I imagine it is not significantly different.)
Everything elsewhere on the Internet says that I need to link my libraries after compiling them to objects, but I believe I am doing that and still getting the error. The command sequence I am using looks like this:
$ dmd -c ./source/mondo/source/* insert.d
then
$ dmd insert.o mondo.o mongoc.o bsond.o
(All of the files above besides insert.o come from ./source/mondo/source/*)
The header to my program looks like this, if it is worth anything:
import mondo;
import bsond;
import mongoc;
I don't see how I can possibly be more explicit to the compiler what I want, but I still seem to get this error.
Note: mondo requires mongo-c-driver to work. I compiled that library with no issues. I tried including it by passing
-L-L/usr/local/lib
to DMD, but that did not work, either. In fact, I just get what appear to be a bunch of "undefined reference" errors for functions in that library as well!

Undefined reference to the function 'check' (GCC compilation in MinGW)

I seem to be getting the rookie error where it says, undefined reference to 'check', as shown below:
This should not be a problem, as I have in fact made a check.h and included in hiker.c, as shown below:
Does anybody know the source of this problem? I have just started using MinGW(as I wanted to learn programming C on Windows).
Here is a picture of the main function. I can add the code too if necessary:
I guess that check function is implemented in a file check.c
You must link that file also, because of your check.h export the prototype to let the compiler know how the check function is structured, but the linker needs the check function code compiled and reachable.
What you need is to compile using a command like this:
gcc -Wall hiker.c check.c -o hiker.exe
Take also note that linker is giving you another error about WinMain#16
This means that you started a windows application project, I guess you must change your project to console project type.

Trying to use the GNU Scientific Library, but weird linker errors occur

so i compiled the GSL from source with Cygwin. Everything went fine, i didnt get any errors. But when i try to compile some simple code, i get the following errors:
..\..\..\Programme\CodeBlocks\MinGW\lib\libgsl.a(error.o) In function `gsl_error':
\usr\include\gsl\err\error.c|43|undefined reference to `__getreent'
\usr\include\gsl\err\error.c|44|undefined reference to `__getreent'
\usr\include\gsl\err\error.c|45|undefined reference to `__getreent'
..\..\..\Programme\CodeBlocks\MinGW\lib\libgsl.a(stream.o) In function `gsl_stream_printf':
\usr\include\gsl\err\stream.c|37|undefined reference to `__getreent'
..\..\..\Programme\CodeBlocks\MinGW\lib\libgsl.a(stream.o) In function `gsl_set_stream':
\usr\include\gsl\err\stream.c|61|undefined reference to `__getreent'
I linked the libgsl.a and libgscblas.a libraries, and there are no complains about any other functions.
For future reference: Dont press cancel instead of Ok when you want to set up a toolchain in code::blocks
The error you're seeing is due to the fact that MinGW doesn't link the Cygwin DLL by default.
MinGW- and Cygwin-compiled libraries don't play that well with each other - while it's possible to get it to work, it's not really a good idea.
Choose one toolchain for your project, which, in this case, means either setting up Code::Blocks so it uses the Cygwin toolchain instead of the bundled MinGW distribution or compiling GSL with MinGW as well.
You may use the following option in Cygwin:
./configure CC=i686-w64-mingw32-gcc
Then you may use GSL in CodeBlocks.

expect - c program

I am trying to compile a program written in c using the expect.h header. I am trying this:
cc -I/usr/include main.c -lexpect -ltcl
cc -I/usr/include main.c
I tried the same with gcc, but to no avail.
I get this:
undefined reference to `exp_spawnl'
undefined reference to `exp_expectl'
Any ideas?
Thanks,
Walter
It means that regardless of what you thought was being used, the expect library being used does not contain those two functions. So, you need to track down which expect library is actually loaded, and then check whether the functions are in it (they aren't, but you should check to see what is defined).
Once you've found your library, you can use nm -g libexpect.so to see the symbols defined. I would expect that you will not see exp_spawnl or exp_expectl in the list of defined symbols.
Then you need to find out which version of the library you do have, and decide whether to upgrade, etc.
I am assuming that the names you quote are indeed spelled correctly.
Is the expect header file installed in /usr/include?
I think the undefined reference error comes from the linker and not the compiler. What directory is the library installed to? Try to link against it using the
-L/path/to/lib -lexpect
option

Resources