Results 1 to 4 of 4

Thread: I don't understand C++ headers

  1. #1
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    [SOLVED] I don't understand C++ headers

    I got some C code I have used in the past, and I want to use it with my first C++ project.

    But when I try to compile, I get some weird errors "undefined reference to <C function>" about every C function I try to use.

    And I did include headers for the C functions, and makefile has the C source files in it as well.

    What's wrong? Does C++ need some namespaces and other crap to work?


    Also, why there isn't .h or .hpp when including C++ standard things?
    Last edited by crazyfuturamanoob; March 11th, 2009 at 05:21 PM.
    Keyboard not found!

    Press any key to continue...

  2. #2
    Join Date
    Jan 2008
    Beans
    1,532

    Re: I don't understand C++ headers

    Quote Originally Posted by crazyfuturamanoob View Post
    I got some C code I have used in the past, and I want to use it with my first C++ project.

    But when I try to compile, I get some weird errors "undefined reference to <C function>" about every C function I try to use.

    And I did include headers for the C functions, and makefile has the C source files in it as well.

    What's wrong? Does C++ need some namespaces and other crap to work?
    That is *way* too vague. There can be any number of things wrong. Please give more info. Also read this excellent article on the topic.

    Quote Originally Posted by crazyfuturamanoob View Post
    Also, why there isn't .h or .hpp when including C++ standard things?
    Because that's what the standards committee decided. I think it's dumb, but we're stuck with it.

  3. #3
    Join Date
    Oct 2007
    Location
    Fort Collins, CO, USA
    Beans
    481
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: I don't understand C++ headers

    C++ does name mangling to implement types. The names of functions and variables are actually remapped into an implementation dependent encoding of the types of the functions, function parameters, and variables.

    To access plain C variables and functions you need to tell the C++ compiler that the declarations are not supposed to have C++ name mangling. Most C headers in /usr/include have the following code in them to make them work with both C and C++.

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /* All of your C declarations go in here. */
    
    #ifdef __cplusplus
    }
    #endif

  4. #4
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    Re: I don't understand C++ headers

    Quote Originally Posted by stroyan View Post
    C++ does name mangling to implement types. The names of functions and variables are actually remapped into an implementation dependent encoding of the types of the functions, function parameters, and variables.

    To access plain C variables and functions you need to tell the C++ compiler that the declarations are not supposed to have C++ name mangling. Most C headers in /usr/include have the following code in them to make them work with both C and C++.

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /* All of your C declarations go in here. */
    
    #ifdef __cplusplus
    }
    #endif
    That's the problem. Thanks.
    Keyboard not found!

    Press any key to continue...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •