This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Calling C++ from C

I'm testing some ideas using the EC++ compiler for C166. I've found that when I want to call a C function from a C++ file I have to prototype the function

extern "C" void myfunc(void);

I'm struggling to find how to call a function in a C++ from a C file. I can see from the m66 file that a function in a cpp file gets named myfunc__Fv. I can prototype my function

void myfunc__Fv(void);

and it compiles OK. Is there a directive that tells the compiler to use the correct naming convention?

(Why? I have a test project which is currently built in plain C. I want to try out some extensions to it in C++. In main have called a function InitCpp which is implemented in InitCpp.cpp - a c++ file. This initialises the mempool, calls __sti__init__ and defines new and delete. However I can't call InitCpp, I get an unresolved external InitCpp since the EC++ compiler calls it InitCpp__Fv. I can fudge it by prototyping as above but wanted to know if there was a way of doing this by design. I cannot find extern "C" in the help since search finds all occurances of C when I try. I was hoping that the manual page describing extern "C" would offer whatever extern "CPP" is. I suspect there nothing unless Keil have modified the plain C complier to support the C++ calling convention.)

Thanks for any ideas
A

IDE-Version:
µVision V4.00a
Copyright (c) Keil Elektronik GmbH / Keil Software, Inc. 1995 - 2009

License Information:
Alec James
Talaris
LIC=xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxxx

Tool Version Numbers:
Toolchain: PK166 Prof. Developers Kit Version: 7.00
Toolchain Path: d:\Keil\C166\BIN\
EC++ Compiler: EC166.Exe V1.09d
C Compiler: C166.Exe V7.00
Assembler: A166.Exe V5.33
Linker/Locator: L166.Exe V5.25
Librarian: LIB166.Exe V4.26
Hex Converter: OH166.Exe V4.7a
CPU DLL: S166.DLL V3.74
Dialog DLL: D167.DLL V2.52

Parents
  • You really shouldn't have posted your license number. Don't know why you did - the license doesn't have anything with your source code to do.

    C++ can call C.
    C can't call C++.

    But you can create a C++ function with C calling convention (remember your extern "C"), making it possible for a C function to call it.

    The other alternative is to compile everything (C and C++ files) in C++ mode. This works unless your code contains any code construct that is only valid in C - after all, C++ is no longer a strict superset of C. If the system header files doesn't contain extern "C" in them, you may have to modify your own code to do:

    extern "C" {
    #include "a_header_file_for_a_c_library.h"
    }
    


    just to make sure that your code will try the C calling convention. But all good C libraries should only have header files that turns on this inside the header files if they detect C++ compilation.

Reply
  • You really shouldn't have posted your license number. Don't know why you did - the license doesn't have anything with your source code to do.

    C++ can call C.
    C can't call C++.

    But you can create a C++ function with C calling convention (remember your extern "C"), making it possible for a C function to call it.

    The other alternative is to compile everything (C and C++ files) in C++ mode. This works unless your code contains any code construct that is only valid in C - after all, C++ is no longer a strict superset of C. If the system header files doesn't contain extern "C" in them, you may have to modify your own code to do:

    extern "C" {
    #include "a_header_file_for_a_c_library.h"
    }
    


    just to make sure that your code will try the C calling convention. But all good C libraries should only have header files that turns on this inside the header files if they detect C++ compilation.

Children