Thread: undefined reference to 'clnt_create'

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    59

    undefined reference to 'clnt_create'

    So the linker (i guess) gives me the following error:

    obj\Release\main.o:main.c undefined reference to `clnt_create'

    I tried adding libc.a to my libraries but that doesn't fix it.

    in CodeBlocks when I do a 'Find declaration of:' it goes to the line in my included clnt.h file:

    extern CLIENT *clnt_create(const char *, const rpcprog_t, const rpcvers_t, const char *);

    Any idea what I've done wrong? I assume it's a common newbie error.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Where in your source code do you provide the function clnt_create?

    If you are not providing the function do not tell the compiler that you are going to provide it.

    Google says the header <rpc/rpc.h> provides a function with the same name.
    If you wish to use that function, I suggest including that header and the library that provides the support for it.


    Tim S.
    Last edited by stahta01; 01-17-2012 at 08:26 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    The whole program is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <memory.h>
    #include "rap.h"
    static struct timeval TIMEOUT = { 25, 0 };
    RAPVAR_READ_NAME_RESP_TYPE *rapvar_read_name_1(RAPVAR_READ_NAME_REQ_TYPE *argp, CLIENT *clnt)
    {
    
        RAPVAR_READ_NAME_REQ_TYPE *inproc = NULL;
        RAPVAR_READ_NAME_RESP_TYPE *outproc = NULL;
    RAPVAR_READ_NAME_RESP_TYPE clnt_res;
    memset((char *)&clnt_res, 0, sizeof(clnt_res));
    
    printf("Hello world!\n");
    
        if (clnt_call (clnt, 1001, (xdrproc_t) inproc, (caddr_t) argp,              /*---CRASH--- */
            (xdrproc_t) outproc, (caddr_t) &clnt_res, TIMEOUT) != RPC_SUCCESS)
            {
                return (NULL);
                printf("clnt_call FAILED!\n");
            }
        return (&clnt_res);
    }
    
    int main()
    {
        int Pclient= 1;
        int pstat=0;
        RAPVAR_READ_NAME_RESP_TYPE *result = NULL;
        RAPVAR_READ_NAME_REQ_TYPE request;
    
        CLIENT *clnt;
        clnt = clnt_create("s4c",300456,1,"tcp");
    
        request.head.userdef=1;
        request.var.name="name";
        request.var.type="robposdata";
        request.var.domain=1;
        request.var.number1=0;
        request.var.number2=26;                                /*ROBTARGET_TRANSFER_TYPE */
    
        result = rapvar_read_name_1(&request, clnt);        /*Call the RPC function */
    
    
        if (result == (RAPVAR_READ_NAME_RESP_TYPE *) NULL)
        {
            printf("'result' is NULL\n");
            pstat = -1;
            return(0);
        }
    
        pstat = result->resp.status;
    
        if (pstat >= 0)
        {
            float *trans = NULL;
            trans[0] = result->resp.RAPVAR_READ_NAME_RESPX_TYPE_u.data.RAPVAR_DATA_TYPE_u.robposdata.robtarget.trans.x;
            trans[1] = result->resp.RAPVAR_READ_NAME_RESPX_TYPE_u.data.RAPVAR_DATA_TYPE_u.robposdata.robtarget.trans.y;
            trans[2] = result->resp.RAPVAR_READ_NAME_RESPX_TYPE_u.data.RAPVAR_DATA_TYPE_u.robposdata.robtarget.trans.z;
    
            printf("\nx = ");
            printf("%f",trans[0]);
            printf("\ny = ");
            printf("%f",trans[1]);
            printf("\nz = ");
            printf("%f",trans[2]);
    
    
     build (RAPVAR_READ_NAME_REQ_TYPE *argp, CLIENT *clnt)
        rapvar_read_name_1( *argp, *clnt)
        print result
            return 0;
        }
    return 0;
    }
    A prototype of clnt_create is located in clnt.h, not rpc.h. however BOTH are 'included' in rap.h:

    Code:
    /*rap.h*/
    /*
     * Please do not edit this file.
     * It was generated using rpcgen.
     */
    
    
    #ifndef _RAP_H_RPCGEN
    #define _RAP_H_RPCGEN
    
    
    //#include <rpc/rpc.h>
    #include "tirpc/rpc/rpc.h"
    #include "tirpc/rpc/clnt.h"
    <...>

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Sounds like clnt.h just has a prototype for the function; a prototype exists for the compiler to perform type checking of arguments and return values as it compiles the code. The actual function code itself however must be created by you or exist in a code library somewhere. If adding libc.a to your project does not resolve the linker error then it does not have the necessary code for that function and it would seem resolution of this problem then falls on you to actually create the function... unless there is some other piece of the puzzle we aren't seeing. There are no other source files you are compiling/linking? No other library files that might contain the actual code for the function?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    Background:
    I'm using Code::Blocks and it's gcc compiler found in it's Mingw directory.
    I'm using RPC headers that came with Cygwin (cygwin/usr/include/tirpc/rpc).
    I found a library that came with cygwin (libtirpc.dll.a) that contains 'clnt_create' but when I want to run it on another PC I have to bring several other cygwin .dlls with it.
    I was under the impression that clnt_create was a standard UNIX function, like clnt_call. windows services for UNIX library has it in librpclib.c but it creates hundreds of errors during linking.

    Currently I am trying to build a .exe that executes a call to a unix based RPC server. Thus far my attempts to create a client handle have failed:

    Code:
    int main()
    {
        int Pclient= 1;
        int pstat=0;
        RAPVAR_READ_NAME_RESP_TYPE *result = NULL;
        RAPVAR_READ_NAME_REQ_TYPE request;
    
        CLIENT *clnt = NULL;
        clnt = clnt_create("s4c",300456,1,"tcp");
        if(clnt==NULL) printf("Client is\tNULL\n");
    
        CLIENT *clnt2 = NULL;
        clnt2 = clnt_create("192.168.1.2",300456,1,"tcp");
        if(clnt2==NULL) printf("Client2 is\tNULL\n");
    <...>
        return(0);
    }
    Client is NULL
    Client2 is NULL

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Mixing binaries (Libraries/object files) from two different compilers can cause issues; you might be better off to just use Cygwin as your compiler if you wish to use its libraries.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    I'd rather use Mingw IF I can find a compatible library with the clnt_create function.
    Any ideas where to look for one? I've already downloaded code:: blocks with mingw and Mingw from mingw.org and they don't have it.
    OR
    Does anyone know how to build a self contained .exe that will work in Windows on other computers WITHOUT having to ferry along a slew of cygwin dlls?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Cygwin uses a BSD socket library like/similar to Linux/Unix/BSD.
    Windows/MinGW uses WinSock2 socket library.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI:

    I found an MinGW header with "clnt_create" in it. The file is named clnt.h and should be included by "<rpc/rpc.h>"

    LibGW32C for Windows

    NOTE: The "libgw32c" package is used to compile code; it may or may not support the functionality that you need.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    THANKS for the find. I'll let you know how it goes.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    So I copied the lib, include and manifest folders from the libgw32c-0.4-lib.zip file into my c:\rap02 directory.
    I added C:\rap02\lib\libgw32c.a to my Link libraries.
    I added C:\rap02\include\glibc to my Compiler, Linker and Resource compiler Search directories.
    This is my build log (Compiler logging: Full command line):



    -------------- Clean: Debug in rap02 ---------------


    Cleaned "rap02 - Debug"


    -------------- Build: Debug in rap02 ---------------


    mingw32-gcc.exe -Wall -g -IC:\rap02\include\glibc -c C:\rap02\main.c -o obj\Debug\main.o
    C:\rap02\main.c: In function 'rapvar_read_name_1':
    C:\rap02\main.c:40: warning: function returns address of local variable
    C:\rap02\main.c: In function 'main':
    C:\rap02\main.c:47: warning: unused variable 'Pclient'
    mingw32-g++.exe -LC:\rap02\include\glibc -o bin\Debug\rap02.exe obj\Debug\main.o C:\rap02\lib\libgw32c.a
    obj\Debug\main.o: In function `main':
    C:/rap02/main.c:53: undefined reference to `clnt_create'
    C:/rap02/main.c:57: undefined reference to `clnt_create'
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    2 errors, 2 warnings

    clnt.h contains only a prototype for the clnt_create funtion.
    The actual definition should be in a library, like a .a or a .dll.
    Apparently the lib (libgw32c.a) does not have the function.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Did you try linking the winsock library as per the website link? It might work; but, I would not bet on it.

    For some functions you must also link with the following standard libraries: - libole32.a - libuuid.a - libwsock32.a
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I created a simple compiler test program.
    I got it to Compile and Link using the standard Cygwin GCC Compiler.

    Tim S.

    Code:
    #include <stdio.h>
    #include <rpc/rpc.h>    /* standard RPC include file */
    
    int main(int argc, char *argv[])
    {
        CLIENT *cl;         /* RPC handle */
    
        /*
         * Create client handle
         */
        if ((cl = clnt_create("s4c",300456,1,"tcp")) == NULL) {
            /*
             * can't establish connection with server
             */
             clnt_pcreateerror("clnt_create");
             exit(2);
        }
    
        clnt_destroy(cl);         /* done with the handle */
        exit(0);
    }
    Code::Blocks build log. My Cygwin base installation folder is "C:/GreenApps/Cygwin1_7"

    Code:
    i686-pc-cygwin-gcc-4.exe -Wall  -g    -IC:/GreenApps/Cygwin1_7/usr/include/tirpc  -c H:/SourceCode/Projects/test-i686-w64/main.c -o obj/Debug/main.o
    i686-pc-cygwin-g++-4.exe  -o bin/Debug/test-i686-w64.exe obj/Debug/main.o    -ltirpc
    Last edited by stahta01; 01-20-2012 at 08:02 PM. Reason: shorten/corrected code sections
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    I got this this morning:



    -------------- Clean: Release in rap00 ---------------


    Cleaned "rap00 - Release"


    -------------- Build: Release in rap00 ---------------


    Compiling: main.c
    cygwin warning:
    MS-DOS style path detected: C:/cygwin/home/eb/rap00/main.c
    Preferred POSIX equivalent is: /cygdrive/c/cygwin/home/eb/rap00/main.c
    CYGWIN environment variable option "nodosfilewarning" turns off this warning.
    Consult the user's guide for more details about POSIX paths:
    Chapter 3. Using Cygwin
    In file included from C:/cygwin/usr/include/tirpc/rpc/rpc.h:67:0,
    from C:/cygwin/home/eb/rap00/rap.h:16,
    from C:/cygwin/home/eb/rap00/main.c:10:
    C:/cygwin/usr/include/tirpc/rpc/svc.h:369:11: warning: ‘struct t_bind’ declared inside parameter list
    C:/cygwin/usr/include/tirpc/rpc/svc.h:369:11: warning: its scope is only this definition or declaration, which is probably not what you want
    In file included from C:/cygwin/home/eb/rap00/rap.h:16:0,
    from C:/cygwin/home/eb/rap00/main.c:10:
    C:/cygwin/usr/include/tirpc/rpc/rpc.h:104:59: warning: ‘struct __rpc_sockinfo’ declared inside parameter list
    C:/cygwin/usr/include/tirpc/rpc/rpc.h:105:35: warning: ‘struct __rpc_sockinfo’ declared inside parameter list
    C:/cygwin/home/eb/rap00/main.c: In function ‘rapvar_read_name_1’:
    C:/cygwin/home/eb/rap00/main.c:40:5: warning: function returns address of local variable
    C:/cygwin/home/eb/rap00/main.c: In function ‘main’:
    C:/cygwin/home/eb/rap00/main.c:47:9: warning: unused variable ‘Pclient’
    Linking console executable: bin\Release\rap00.exe
    obj/Release/main.o: file not recognized: File format not recognized
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 1 seconds)
    0 errors, 6 warnings

    And when I looked rap00.exe did not exist anywhere.
    So I switched every compilers I was using in my toolchain to whatever looked compatible in my c:/mingw install.
    It compiles/runs with no errors but still fails to create a client (returns a NULL).
    I am also using cygwin's libtirpc.dll.a library and it's dependencies because it's the only library I can find that defines the clnt_create function.

  15. #15
    Registered User
    Join Date
    Jan 2012
    Posts
    59
    btw - thanks again for trying to help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference
    By Dudealadude in forum C Programming
    Replies: 1
    Last Post: 11-29-2011, 02:43 PM
  2. undefined reference to
    By diego in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2010, 03:06 PM
  3. Undefined reference to...
    By legendus in forum C Programming
    Replies: 19
    Last Post: 10-25-2009, 06:18 AM
  4. Undefined reference?
    By Tirith in forum C Programming
    Replies: 8
    Last Post: 08-11-2009, 02:39 PM
  5. Undefined Reference
    By xshapirox in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2004, 01:22 PM