Having Some Trouble With Adding A Library?

This is a discussion on Having Some Trouble With Adding A Library? within the C Programming forums, part of the General Programming Boards category; Hi, I'm very new to C and I'm following on online course. I'm aware that there may have been other ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    Having Some Trouble With Adding A Library?

    Hi, I'm very new to C and I'm following on online course. I'm aware that there may have been other threads similar to this but I can't understand the solutions for those threads. I'm following the CS50 course, so I was given two files, cs50.c and cs50.h. I am aware the ".c" file is a C file, and the ".h" is a header file, but I'm not sure what to do with them. In the cs50.c file there is something that says this:

    * To compile as a static library on your own system:
    * % gcc -c -ggdb -std=c99 cs50.c -o cs50.o
    * % ar rcs libcs50.a cs50.o
    * % rm -f cs50.o
    * % cp cs50.h /usr/local/include
    * % cp libcs50.a /usr/local/lib

    I'm confused as to where to enter in this code, if anybody could explain that to me that would be great. I'm a PC user and I code using Code::Blocks.

    Thank you.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    That's not done in the code, it's done at the command line.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    That's what I'm confused about. Where is the command line? I know that must sound stupid but I'm very new . Thanks.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Whatever course you're following is assuming you're using Linux or Unix or a similar system. If you're not, you're likely to stay stumped.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    You don't think there is any way to do it if I'm running Windows?

    Here are the two files I was talking about before. First one is the C file, and the second is the Header.

    https://www.cs50.net/pub/releases/cs50/cs50.c

    https://www.cs50.net/pub/releases/cs50/cs50.h

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It looks like they are just providing the source code which you must then transform into a library. They expect you to do that from the command line but since you are on Windows, you could do it using codeblocks. Just make a new project and select STATIC LIBRARY from the options that appear. Add the two files to the project and then build it. Codeblocks should automatically build the library file libcs50.a for you in the folder where the project is.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    Thank you so much claudiu, did exactly what you said and it worked.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Glad you figured it out mate.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. standrad library
    By sarahr202 in forum C++ Programming
    Replies: 11
    Last Post: 05-18-2009, 08:50 PM
  2. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 02:44 PM
  3. Replies: 19
    Last Post: 01-12-2006, 10:04 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  5. adding library checks in configure.in
    By rohit in forum Linux Programming
    Replies: 1
    Last Post: 09-01-2002, 05:04 AM

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