"Undefined Reference"

This is a discussion on "Undefined Reference" within the C Programming forums, part of the General Programming Boards category; Hello all, when I compile this with gcc on redhat 8, it passes the compilation but gets errors in linking ...

  1. #1
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94

    "Undefined Reference"

    Hello all, when I compile this with gcc on redhat 8, it passes the compilation but gets errors in linking that say:
    -------
    xxxx.o-undefined reference to BF_set_key
    xxxx.o-undefined reference to BF_ecb_encrypt
    -------
    those two functions are in an include file... do I need to tell the main section that they exist?

    Here's the code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <openssl/blowfish.h>
    
    int main(int argc, char* argv[]) {
    
      BF_KEY *tkey;
      int ilen;
      char *data;
      unsigned char *in[64],*out[64];
    
      strncpy(in,"hello",sizeof(in));
      ilen=5;
      strncpy(data,"abcde",sizeof(data));
    
      BF_set_key(tkey,ilen,data);
    
      BF_ecb_encrypt(in,out,tkey,BF_ENCRYPT);
    
      return 0;
    }
    Thanks All,
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  2. #2
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    Oh, by the way, I'm trying to do blowfish encryption/decryption. If anyone knows of a better way to do this (i would like to be able to use this code for solaris as well), please feel free to suggest them to me.

    Thanks again,
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Are you linking with the library? I think it's libcrypto:
    Code:
    gcc blah blah -llibcrypto
    My best code is written with the delete key.

  4. #4
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    gcc -o blowfish blowfish.c -llibcrypto says :"cannot find -llibcrypto"

    I also tryed moving "-llibcrypto" around a little in the string, still same thing.

    I don't think I need to get the library, the functions are in the blowfish.h, at least that's what "man blowfish" tells me.

    thanks
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Then you'll need to find out what the library file is called and do -llibrary when linking.
    My best code is written with the delete key.

  6. #6
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    I'm pretty sure the man page said it didnt need a lib but i will look around.

    thanks
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm pretty sure the man page said it didnt need a lib
    The error you're getting basically says that while the declarations for those functions were found (thus allowing you to compile), the definitions weren't found, so the object code couldn't be linked into a binary because it didn't exist.
    My best code is written with the delete key.

  8. #8
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    hm ok. Do you know where the libs are stored so i can look through to find the right one? or do you know another way to do blowfish?

    thanks,
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Do you know where the libs are stored so i can look through to find the right one?
    I'm afraid I don't. But you could search the OpenSSL documentation to see if it say anything. Also, try -lcrypto.

    >or do you know another way to do blowfish?
    You could write it yourself. Google should turn up the basica algorithm.
    My best code is written with the delete key.

  10. #10
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    Quote Originally Posted by Prelude
    >or do you know another way to do blowfish?
    You could write it yourself. Google should turn up the basica algorithm.
    Well I don't think I quite have the skills yet to undertake that. I did find three good looking implementations on google, however, i can't get them to work, but's I think it's my fault, and I didn't really want to post that I didn't know how to use it. I was hoping to not be quite THAT noobie-ish.

    Thanks,
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  11. #11
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    It should be in libcrypto.so, so you would
    gcc -o blah blah.c -lcrypto
    That is if you have openssl installed.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  12. #12
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    when i do a -lcrypto, the "cannot find lcrypto" goes away, but apparently the right stuff isn't in lib crypto, since it still says undefined reference.
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  13. #13
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    first you have to include string.h
    You shouldn't pass char *blah[64] to strncpy it should just be char blah[64]. You don't need stdlib.h.
    I got to compile the code. But it segfaulted. I wil look into it.

    [EDIT] One reason I just noticed is data is a *ptr and you strncpy to it without mallocing it[/EDIT]
    Last edited by linuxdude; 05-26-2004 at 03:39 PM.

  14. #14
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    Quote Originally Posted by linuxdude
    first you have to include string.h
    Hm It didn't give me any error on that but sounds true enough.
    Quote Originally Posted by linuxdude
    You shouldn't pass char *blah[64] to strncpy it should just be char blah[64].
    I know, but the man page for the blowfish funcs said that it wanted a pointer, so I defined the variable as such. I have to say I'm not fully comfertable with using pointers as opposed to regular vars.
    Quote Originally Posted by linuxdude
    You don't need stdlib.h.
    I know, but I just set up the header because I knew I would need it later in my program, once I get the basics to work.
    Quote Originally Posted by linuxdude
    I got to compile the code. But it segfaulted. I wil look into it.
    I'm thinking maybe it segfaulted because the func takes a 64-length char array, and I passed it one, thus not leaving a terminating null?

    Thanks very much,
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  15. #15
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    did you get it to compile. The reason it didnt' tell you you need to include <string.h> you have to add these two options to your gcc compilation.
    Code:
    gcc -o test test.c -Wall -pedantic
    It would be nice to get into this habit, because it would tell you you undefined reference to strncmp. Let me know how it turned out

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error "undefined reference to"
    By Bargi in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2007, 06:12 AM
  2. "undefined reference" in my Class
    By bumcheekcity in forum C++ Programming
    Replies: 8
    Last Post: 04-08-2007, 02:40 PM
  3. Replies: 5
    Last Post: 06-01-2006, 05:37 PM
  4. Replies: 11
    Last Post: 12-14-2005, 02:09 AM
  5. TAPI "undefined reference"
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-16-2002, 09:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21