locked
Using C functions in VC++ RRS feed

  • Question

  • Hi,
    I have created a project in VC++, which interfaces with a miccrocontroller through C functions. These C functions are written in a combination of several C, C++ files which i need to include in my project.Can anyone tell me what's the best way of going about this?
    I have tried adding all the files to the workspace,but can't use the C functions in my VC++ files...
    Friday, March 23, 2007 9:13 AM

Answers

  • What do you mean by "can't use the C functions in my VC++ files". If you add the C files to your project as *.c files (not *.cpp, *.cxx, *.cc) then the project system should automatically compile them as C.

    You C++ files, I am assuming, should include a *.h file that declares the C methods.

    Friday, March 23, 2007 3:37 PM
  •  Jonathan Caves - MSFT wrote:

    You C++ files, I am assuming, should include a *.h file that declares the C methods.

    To add to Jonathan's point, you may find that you need to use extern "C" appropriately in the header files so that VC++ knows to use C Language naming conventions in linking from the C++ code.  You can find out more about this in MSDN Help and also by inspecting small header files in the VC++ and PSDK include directories. 

     - Dennis

    Friday, March 23, 2007 6:03 PM
  •  ArwaPatel wrote:
    Hi,
    I have created a project in VC++, which interfaces with a miccrocontroller through C functions. These C functions are written in a combination of several C, C++ files which i need to include in my project.Can anyone tell me what's the best way of going about this?
    I have tried adding all the files to the workspace,but can't use the C functions in my VC++ files...

     

    The way to approach this sort of problem is to do a little exploratory programming.

    Create a file called foo.c

    foo(double x)

    {

      int private = sizeof(' A');

      return private

    }

    This function is designed to be legal C and to break C++. It should return sizeof(int), not sizeof(char).

    Now create a header file, and try to call it from your C++ program. Fiddle with #ifdef __cplusplus es and extern "C" { } s until you get it to work correctly. Then you should be able to do the same thing with your real C code. 

     

    Saturday, March 24, 2007 10:03 AM

All replies

  • What do you mean by "can't use the C functions in my VC++ files". If you add the C files to your project as *.c files (not *.cpp, *.cxx, *.cc) then the project system should automatically compile them as C.

    You C++ files, I am assuming, should include a *.h file that declares the C methods.

    Friday, March 23, 2007 3:37 PM
  •  Jonathan Caves - MSFT wrote:

    You C++ files, I am assuming, should include a *.h file that declares the C methods.

    To add to Jonathan's point, you may find that you need to use extern "C" appropriately in the header files so that VC++ knows to use C Language naming conventions in linking from the C++ code.  You can find out more about this in MSDN Help and also by inspecting small header files in the VC++ and PSDK include directories. 

     - Dennis

    Friday, March 23, 2007 6:03 PM
  •  ArwaPatel wrote:
    Hi,
    I have created a project in VC++, which interfaces with a miccrocontroller through C functions. These C functions are written in a combination of several C, C++ files which i need to include in my project.Can anyone tell me what's the best way of going about this?
    I have tried adding all the files to the workspace,but can't use the C functions in my VC++ files...

     

    The way to approach this sort of problem is to do a little exploratory programming.

    Create a file called foo.c

    foo(double x)

    {

      int private = sizeof(' A');

      return private

    }

    This function is designed to be legal C and to break C++. It should return sizeof(int), not sizeof(char).

    Now create a header file, and try to call it from your C++ program. Fiddle with #ifdef __cplusplus es and extern "C" { } s until you get it to work correctly. Then you should be able to do the same thing with your real C code. 

     

    Saturday, March 24, 2007 10:03 AM
  • Hi,
    thanks for the reply.. for some practice i made a small C program with simple

    functions and a header file for it. added the header file to my MFC project in

    the extern "C" block...It was working perfectly... I will try the same for the

    Main project too.. Like u suggested i am going to create header files for my C

    source files.

    Monday, March 26, 2007 10:28 AM