Including my own header files

This is a discussion on Including my own header files within the C Programming forums, part of the General Programming Boards category; Hey everyone! I am following a course in iTunes U teaching C programming for fun, and I am completely new ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    Including my own header files

    Hey everyone!

    I am following a course in iTunes U teaching C programming for fun, and I am completely new to the language. There is a header file specific to the course for specific functions used in the coursework. Most of the command line work is being done in Linux, however I am doing my work in the Terminal in Mac OS X (Unix, obviously). Presently, I can't seem to #include the header file to have my code compile correctly! I have placed the header file in the folder I am presently working in, and in nano I have typed:


    Code:
    #include <stdio.h>
    #include "cs50.h"
    
    int
    main (int argc, char * argv[])
    {
            string name;
            printf("State your name: ");
            name = GetString();
            printf("hello, %s\n", name);
    }
    I then type

    gcc -o hello3 hello3.c -cs50
    at the command line. I end up with this error:
    Undefined symbols:
    "_GetString", referenced from:
    _main in ccUiANy0.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    GetString is one of the functions that is a part of this class specific library, and I am using a code example used by the instructor, so I should be using it correctly. Would anyone be able to point me in the right direction to what I could be doing wrong?

    Thank you ahead of time for any help!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Try this:

    Code:
    gcc -o hello3 hello3.c -lcs50

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    I had actually tried that, as that is what the instructor originally had the students doing. This returns:

    Code:
    ld: library not found for -lcs50
    collect2: ld returned 1 exit status
    the two files that are located within the directory I am working within are cs50.h and cs50.c
    (just FYI)

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Oh, well then you need to compile it manually.

    Code:
    gcc -o hello3 hello3.c cs50.c
    That should have been obvious since you have two .c files.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    gcc -o hello3 hello3.c -cs50.c
    returns

    Undefined symbols:
    "_GetString", referenced from:
    _main in cckEgUIQ.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    Yeah, I am REALLY brand new Just trying to get through the beginning, didn't think of trying that, but this is what it returns.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    Woah!!! I didn't look closely enough! It worked without the lil "-"

    May I ask what that means? Why it works that way and what "manually" is?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    gcc -o hello3 hello3.c cs50.c
    Basically means: compile hello3.c and cs50.c, then link the object files generated to output the executable program hello3.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2009
    Location
    Florida
    Posts
    15
    I am also trying to follow the same lessons from cs50.net and having the same problem. The only difference is that I am trying to use Code::Block under Windows XP x32.

    I have downloaded these two files

    cs50.h
    cs50.c

    from here:

    Index of /pub/releases/cs50

    I figured I have to put the cs50.h file into this folder:

    C:\Program Files\CodeBlocks\MinGW\include

    What do I do with the other file?

    I am missing something for sure because if I try to include that library (or whatever) by typing:

    #include <cs50.h>

    I get these errors below. So what am I doing wrong?

    Thanks a bunch for any help!


    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|1|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|24|error: invalid token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|25|error: invalid token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|27|error: invalid token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|27|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|27|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|28|error: invalid token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|28|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|28|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|38|error: invalid token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|38|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|38|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|50|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|64|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|79|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|94|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|109|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|124|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|138|error: expected unqualified-id before '<' token|
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4. 5\..\..\..\..\include\cs50.h|138|error: invalid token|
    ||=== Build finished: 20 errors, 0 warnings ===|

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a new thread. There's no reason to bump a two year old thread just because you're having problems compiling.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Location
    Florida
    Posts
    15
    Quote Originally Posted by quzah View Post
    Make a new thread. There's no reason to bump a two year old thread just because you're having problems compiling.


    Quzah.
    Most forums they complain if you make a new thread when there is a similar. Hence.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's two goddamn years old! If everyone did this, we'd have ten threads on the forum, each one ten years old and a thousand pages long!


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Nov 2009
    Location
    Florida
    Posts
    15
    Quote Originally Posted by quzah View Post
    It's two goddamn years old! If everyone did this, we'd have ten threads on the forum, each one ten years old and a thousand pages long!


    Quzah.
    Whaaaa.... thanks for the warm welcome bro! I hope you are having an ok day otherwise.

    Ok I will repost it.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Given that you responded to the post that has your answer in it, the point is less "you need to make a new post" and more "why did you even post at all"?

    (Note that Code::Blocks uses gcc as the compiler, so the command-line you were given is what you need to use. You can force C::B to use that command line, if you put everything in a project.)

  14. #14
    Registered User
    Join Date
    Nov 2009
    Location
    Florida
    Posts
    15
    Quote Originally Posted by tabstop View Post
    Given that you responded to the post that has your answer in it, the point is less "you need to make a new post" and more "why did you even post at all"?

    (Note that Code::Blocks uses gcc as the compiler, so the command-line you were given is what you need to use. You can force C::B to use that command line, if you put everything in a project.)
    You know at least you are actually trying to help.

    I did see this mentioned here and that we need to do that manually:

    gcc -o hello3 hello3.c cs50.c

    But how do I force C::B to do that not manually? That is my question, and what do I do with the cs50.c file? Where does that go? It seems the compiler is looking for that file. Where is that answered in this thread?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Huncowboy View Post
    You know at least you are actually trying to help.

    I did see this mentioned here and that we need to do that manually:

    gcc -o hello3 hello3.c cs50.c

    But how do I force C::B to do that not manually? That is my question, and what do I do with the cs50.c file? Where does that go? It seems the compiler is looking for that file. Where is that answered in this thread?
    Do you normally put files in bizarre places? And for that matter, if you insist on being beholden to the mouse and decide to include it in your project you can put it anywhere you want, since you'll have to go through the file picker to put it in your project so you just have to know where it is. Nobody else cares where it is.

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

Similar Threads

  1. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 03:53 PM
  2. Including header files
    By Emeighty in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2008, 03:02 PM
  3. Header files in .h
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 08:58 AM
  4. Class files including other classes
    By combatdave in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2006, 11:37 PM
  5. more header files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2001, 12:56 PM

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