functions in .C file not recognized in .CPP file

This is a discussion on functions in .C file not recognized in .CPP file within the C++ Programming forums, part of the General Programming Boards category; Hi, this is my first post on these forums. I have just recently started working on a C++ application that ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    Question functions in .C file not recognized in .CPP file

    Hi, this is my first post on these forums.

    I have just recently started working on a C++ application that was started by someone else. When I first checked out the project from CVS, I was getting compile errors in one of the C++ files. The file had function calls to functions defined in a .C file, and the compiler said these functions were undefined. I changed the .C file to a .CPP file, and now the compiler recognizes the functions.

    Is there anything wrong in doing this? The C code compiles perfectly in the .CPP file. Is it possible that the code may work differently when it runs?

    The compiler I am using is MinGW g++ with Eclipse and the CDT plugin as the IDE. Is there some way to configure the compiler to recognize the functions in the .C file?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > The file had function calls to functions defined in a .C file
    C++ compilers mangle names (to support overloading), whereas C does not. If a C++ compiler tries to call a C function, you need to stop it mangling the name.
    This is usually done by having
    Code:
    extern "C" {
      void some_c_func();
    }
    around the prototypes of the C functions.

    > Is there anything wrong in doing this?
    Is there anything to indicate that it should be a C file, or just plain sloppyness on the part of the original author (could be hard to tell). Like can you ask them about it?
    Compiling a project in a single language should be preferred, so if it seems to work, go for it.

    > Is it possible that the code may work differently when it runs?
    It's possible, there are some things which compile differently in C and C++, but are valid in both languages.
    http://david.tribble.com/text/cdiffs.htm
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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