Thread: including c librarys

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    including c librarys

    hello

    i think i am being silly but i cannot figure out how to include a library i have downloaded to use.
    i got the openssl librarys as i wish to use some bits from them but how do i include them in my c file to work when i compile it and to use the functions in them?
    i see people reference bits from it as openssl/file.h but i am confused about how to implement it.

    thank you.
    terrak

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Lookup the "-l" option in the manpage of your compiler.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    thank you for that. Where do i put the library i want to use in relation to my code? will that work as long as i have make'ed and installed the openssl library?

    thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    gcc -I/path/to/header/files prog.c -L/path/to/library -lnameOfLib

    Where the actual library you want to link against is
    /path/to/library/libnameOfLib.a

    If you have a #include of
    #include <foo/bar.h>

    Then the full path will be
    /path/to/header/files/foo/bar.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    hello

    thank you for your replies, they have helped me but I have found another problem.
    i have changed to the mhash library and this has installed correctly. the library is found under
    /usr/local/lib - this holds the libmash.a, .la, .so etc files
    so when compiling i use

    gcc -L/usr/local/lib -o "filename" ./filename.o -lmhash

    it now compiles absolutely fine but when i run "filename" it displays the error:
    /home/user/filename/Debug/filename: error while loading shared libraries: libmhash.so.2: cannot open shared object file: No such file or directory
    the .so.2 file also exists in /usr/local/lib.

    am I still doing something silly with this somewhere? it is happy with the references in the code, now it just won't access the library externally it seems.

    thanks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Check your spelling!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you do
    Code:
    ldconfig -p | grep "libmhash.so.2"
    what do you see?

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    Hello

    sorry, i meant libmhash.a near the start of my post, everything else was pasted from either a terminal or ide.
    when i try that last command it shows nothing. looking at the man page of that command do i need to run that to include this library for my executing code to work?

    thanks

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    Thank you for your help.
    I did some investigation on ldconfig and this pointed me at /etc/ld.so.conf.d/libc.conf
    this had the path in that contained the library i needed to use.
    I then ran ldconfig -p under sudo and it output alot, i run the same with | grep mhash and it is suddenly showing.
    When you install libraries is this a command that needs to be run manually to re-index any libraries you have installed?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Terrak View Post
    Thank you for your help.
    I did some investigation on ldconfig and this pointed me at /etc/ld.so.conf.d/libc.conf
    this had the path in that contained the library i needed to use.
    I then ran ldconfig -p under sudo and it output alot, i run the same with | grep mhash and it is suddenly showing.
    When you install libraries is this a command that needs to be run manually to re-index any libraries you have installed?
    Yes. (Package managers, like syn and rpm etc, will run this for you; but one way or another it needs to run.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including extra .cpp files visual2008
    By Cathalo in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2009, 03:29 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Class files including other classes
    By combatdave in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 12:37 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Reading a line including blanks
    By Ec4U2du in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 07:32 PM