#include help

This is a discussion on #include help within the C Programming forums, part of the General Programming Boards category; I'm following along with the Harvard cs50 lectures. Using a mac OS, I have downloaded VirtualBox, and installed cs50 library ...

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

    #include help

    I'm following along with the Harvard cs50 lectures. Using a mac OS, I have downloaded VirtualBox, and installed cs50 library from https://manual.cs50.net/CS50_Library (I typed the Linux, Fedora code into the terminal in gedit). However I still cannot seem to use the functions when I have #include <cs50.h> after compiling

    I get undefined reference to "GetString"

    Please help
    Thanks you!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    When you compile, just tell your compiler to look in the local directory where the header is. The #include with angle brackets (<>) searches in the compiler's include directory by default, and your file is not there.

    In gcc (and I would try with other compilers) you have to add:
    -I"path/to/header"
    but in different compilers there are different switches, so you should double check. If you still get "undefined reference" linker errors, then -I is not the switch.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,898
    Also you must tell the linker to link with the proper library. Undefined reference errors usually mean the linker can not find the function implementations. Normally if the compiler can not find the include file the error message would be about function not being declared.

    Jim
    Last edited by jimblumberg; 01-03-2012 at 11:28 PM.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Thanks both for your reply.

    In terminal I am typing gcc name.c -I"user/local/lib/cs50.h" and am still getting undefined reference error.

    I have only written a hello world with the stdio.h so I am very new

    Is the stdio.h stored in the computer, where I can but the cs50.h file there too?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Do you only have the library header? You also have to link against code.

    Precompiled libraries will have the extension .a or .so, or .lib on windows. If you find those, you need to link them with -l (ell) like jim said. If you put it in the -I directory then you shouldn't have a problem.

    If you have a C file with library code, all you have to do is add it to your project.

    I don't think a teacher would provide just a header, so you need to double check that you have everything you need. But at the moment you're stuck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does #include indirectly include the source file too?
    By Lord Asriel in forum C Programming
    Replies: 10
    Last Post: 11-30-2011, 08:20 AM
  2. Replies: 1
    Last Post: 11-06-2011, 06:20 PM
  3. basic difference between #include<> and #include""
    By gunjansethi in forum C Programming
    Replies: 1
    Last Post: 03-26-2010, 01:53 AM
  4. #include <windows.h> and #include <wininet.h>
    By steve1_rm in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2009, 12:14 PM
  5. Which came first? #include <stdio.h> or #include <stdlib.h> ?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-14-2002, 10:58 PM

Tags for this Thread


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