0

I downloaded a library from here. I added the header file in my program but when I try to access the functions of this header file, I get error:

undefined reference to the function for u_open() and u_accept(). I tried to compile the .c files of this header file but I get this error:

undefined reference to main.

I tried everything in my knowledge to solve this issue, but couldn't solve it. Here is the program.

#include "uici.h"
int main()
{
char client[50];
char buf[1024];

u_port_t portnumber;
portnumber=48625;

int fd = u_open(portnumber);

int communFd = u_accept(fd,client,50);

perror("Opened");

fprintf(stderr,"\nComun fd is %d\n\n\n",communFd);

read(communFd,buf,1024);

write(STDOUT_FILENO,buf,1024);

fprintf(stderr,"\n\nReading complete\n");
return 0;
}

What can I do to solve this problem?

Regards

4
  • 2
    You need to link to the library while compiling. Including the header file only enables the compiler to know the declaration of the function it does not provide the function definition which is defined in the library. Commented Jan 28, 2013 at 7:50
  • For that you have to tell us the compiler you are using.That is precisely the reason I haven't posted that as an answer. :) Commented Jan 28, 2013 at 7:52
  • gcc compiler. The one builtin to the OS Commented Jan 28, 2013 at 7:54
  • This is a duplicate post, there is already plenty of information on how to link files. Commented Jan 28, 2013 at 8:09

3 Answers 3

1

Your header file uici.h declares the functions you're calling in main() (u_open() and u_accept()), but not their implementations, which are in the source files. When the linker tries to create the entire program, it then complains that the implementations can't be found.

The solution is to link all the files together when creating the actual program binary. When using the g++ frontend, you can do this by specifying all the source files together on the command line. For example:

g++ -o main main.c uici.c

will create the main program called "main", assuming that the implementations you need are in uici.c.

edit: In the case you're linking against a prebuilt library for uici, you'll need to specify to the frontend that the library is needed for linking, e.g. with:

g++ -o main main.c -luici
Sign up to request clarification or add additional context in comments.

Comments

0

You need to link the library when using gcc like :

gcc nameofprgram.c -l<library>

3 Comments

what like this:gcc server.c -l<uici.h> -o serv
no find out the name of the library. For example if the name of the library is hello then you have to use -lhello. uici.h is not the library its just a header file containing declaration of functions.
what is the name of your library that you downloaded ?
0

Use any of these flags while compiling with gcc

-I <searchpath to include files>
-L <searchpath to the lib file>
-l<thelibname>

Ex:

  gcc -o myprogram -lfoo -L/home/me/foo/lib myprogram.c

This will link myprogram with the static library libfoo.a in the folder /home/me/foo/lib

8 Comments

both the program and library file are present in the same folder. should I do like this:gcc server.c -l<uici.h> -o serv
gcc -o serv -l<uici.h> server.c
did u type like above? use the library name with the -l flag. like given in the example above. or if you want to point to header files. use -I <searchpath to include files.
I copied and pasted your provided statement and I am getting this error
I don't know that. I don't think there is any prebuilt library
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.