compiling cpp in mostly c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • divideby0
    New Member
    • May 2012
    • 131

    compiling cpp in mostly c

    I've got a large C project that I'm too far along in to abandon altogether, but I'd like to use some basic cpp functionality. Is it possible to mix these because compilation fails whenever I try to call a cpp function.

    Code:
    // main.h
    
    #ifndef __MYHEADER__
    #define __MYHEADER__
    
    // std c includes
    
    // defines
    
    // cpp prototypes
    void setVector(const int &, const int &, const unsigned char &);
    // compiler didn't like this, so changed to
    void setVector(const int, const int, const unsigned char);
    
    #endif
    Code:
    // vec.cpp
    #include <vector>
    #include "main.h"
    
    using std::vector;
    // declared global vector here
    
    void setVector(const int row, const int col, const unsigned char ch)
    {
       myvec[row][col] = ch;
    }
    the actual call comes from a c source file, but when I compile the project, I get "unresolved external symbol _setVector referenced in function _WndProc@16". I know what this error means, but how would I fix it? compile and link separately?

    I'm using visual C++ express 2008.

    TIA
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The error means you have created your project as Windows application and you have no WndProc().

    Delete your project and create a new one. Your C/C++ properties under the Advanced tab should have at least
    /D "WIN32" for 32-bit addressing. Your vector will work fine as 32-bit.

    That assumes you want a Windows program. If not, like you were wanting a console application with a main() then:

    1) Create a new Win32 project
    2) When the wizard appears do not click Finish
    3) Instead click Application settings
    4) Select empty project and console application
    5) Now click finish and off you go.

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      It's api - no mfc or resource files. I created a new cpp file and added it to the otherwise all c project; setVector was called from within the default wndproc wm_command message.

      Code:
      case WM_COMMAND : 
         
            switch(LOWORD(wParam)) {
               // process control ids
            }
       
            setVector(...);  
      ...
      }
      The app compiles and runs with the reference to setVector commented out.

      The project has main.h and three source files

      main.c - winmain
      proc.c - default proc
      vect.cpp - the function definitions and declarations for the vector

      Under each source file's properties, I selected compile as c for the c files and compile as cpp for the cpp.

      under project properties, the c/c++ command line reads

      /Od /D "_MBCS" /Gm /EHsc /RTC1 /MDd /Fo"Debug\\" /Fd"Debug\vc90.p db" /W3 /nologo /c /ZI /TC /errorReport:pro mpt

      other than disabling precompiled headers and setting the subsystem to windows, every option was left at its defaults.

      When I created the project, I selected "general" from types and "empty project" from templates. Should I choose Win32 for both instead?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Do you have a proc.h and a vect.h?

        You will need in main.c:

        Code:
        #include <proc.h>
        #include <vect.h>
        These headers are to contain the function prototypes of the functions you call in main.c. Without those prototypes, your calls will be marked "unresolved external reference".

        Remember, each .c must be able to compile on its own. That means main.c must contain the code for the function or the function prototype.

        It is the job of the linker to match your function call with your code for the function.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You are missing the extern "C" declaration in you header file that causes the C++ compiler to use the C naming convention instead of the C++ naming convention which mangles the names to support function overloading.

          Because of this where you include main.h into a cpp file setVector gets a C++ name and where you include it into a C file (main.c containing WndProc ???) it gets a C name and then the linker can't match them up so you get an unresolved external.

          try

          Code:
          // main.h
           
          #ifndef __MYHEADER__
          #define __MYHEADER__
           
          // std c includes
           
          // defines
          
          // function prototypes
          #if __cplusplus
          extern "C" {
          #endif
          
          void setVector(const int, const int, const unsigned char);
          
          #if __cplusplus
          }
          #endif
           
          #endif
          This declares setVector as having C calling convention even in cpp files and should allow the function in vec.cpp to compile and be called from C language files.

          Comment

          • divideby0
            New Member
            • May 2012
            • 131

            #6
            Thank you both; weakness, you've always been a huge help and banfa, I had no clue about extern "C" - your explanation was most helpful and did the trick.

            I'm always learning something new here.

            Comment

            Working...