Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

extern

magnum818magnum818 Posts: 27Member
1.) Can some one tell me what the "extern" keyword is good for and what it means.
2.) How do I wrap a c++ method call in a c function, so that I can use my c++ code in a c program?
Thank in advance

Comments

  • sigma_zksigma_zk Posts: 68Member
    [italic]1.) Can some one tell me what the "extern" keyword is good for and what it means.[/italic][hr]

    I'm not too good remembering all the peculiarities of scoping, but to the best of my knowledge, the 'extern' keyword is usually used to "redeclare" an external variable:

    [code]int x;

    int main()
    {
    extern int x;
    }[/code]

    This is usually done when you want to use a variable defined in another file.

    It is also used to direct the C++ compiler not to mangle a function name from a C library:

    [code]extern "C" foo();[/code]

    I think there is a third use that has to do with static vars, but I forget what it is :P.


  • JBakerJBaker Posts: 197Member
    : 1.) Can some one tell me what the "extern" keyword is good for and what it means.
    : 2.) How do I wrap a c++ method call in a c function, so that I can use my c++ code in a c program?
    : Thank in advance
    :
    [green]1) Use the extern modifier to indicate that the actual storage and initial value of a variable, or body of a function, is defined in a separate source code module. Functions declared with extern are visible throughout all source files in a program, unless you redefine the function as static.

    2) To use C++ moduals in a C project you will have to add this to your header file that needs it. Remove ... from the function prototype that use it and replace it with __CPPARGS.
    [code]
    #ifdef __cplusplus // Using C++ compiler.
    #define __CPPARGS ...
    #else // Using C compiler.
    #define __CPPARGS
    #endif[/code]
    You will also have to set your compiler options and let it know your going to be doing this. Some functions (like interrupt handlers) will have to be prototyped differently as well. You can use #ifdef __cplusplus for this too.

    You have to also keep in mind that certain thing will not be allowed (like nested comments). In my opinion it's always best to pick one language and do the entire project for that one.[/green]
    [hr]
    [blue][size=4]JBaker[/size][/blue]

  • DB1DB1 Posts: 1,142Member
    : 1.) Can some one tell me what the "extern" keyword is good for and what it means.
    : 2.) How do I wrap a c++ method call in a c function, so that I can use my c++ code in a c program?
    : Thank in advance
    :



    This is one method that I use, it might not be the best way but it works. This shows a use of the "extern" keyword as well as answering your question #2.


    Say I have a C++ file, and I want to be able to use the functions in my C program... I need to first define some external functions that I can call from a normal C file. These external functions in turn will call functions in my C++ class:
    [code]
    [green]// some_file.cpp[/green]

    #include "some_file.h"

    class some_class {
    public:
    int some_function();
    float some_other_function();
    } MyClass;

    int some_class::some_function()
    {
    return 1;
    }

    float some_class::some_other_function()
    {
    return 1.0f;
    }

    [green]// external functions for use in a C program[/green]
    int call_some_function()
    {
    return MyClass.some_function();
    }

    float call_some_other_function()
    {
    return MyClass.some_other_function();
    }
    [/code]

    Now I need a header file that I can include in my C files. I need to declare the functions as extern "C" so the C++ compiler will treat them as C functions...
    [code]
    [green]// some_file.h[/green]

    #ifndef SOME_FILE_INCLUDED
    #define SOME_FILE_INCLUDED

    #ifdef __cplusplus
    extern "C" {
    #endif

    int call_some_function();
    float call_some_other_function();

    #ifdef __cplusplus
    }
    #endif

    #endif [green]// SOME_FILE_INCLUDED[/green]
    [/code]

    Ok, now I just add the line:

    #include "some_file.h"

    in my C program file that I want to use the functions, and add the new C++ file and header to my project. Also, if I'm using gcc to compile the program I'll have to change my makefile to use g++ compiler instead.

    Now I can use those functions prototyped in my new .h file in my C program. :-)




    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

Sign In or Register to comment.