import an external function

Discussion in 'C++' started by Maximus, Sep 8, 2008.

  1. Maximus

    Maximus Guest

    How to import a function in another translation unit with a namespace? Let's
    say we have two cpp files:

    1.cpp

    namesapce mu {

    int func() { return 0;}

    };


    and in 2.cpp, I declare

    namespace mu {

    extern int func();

    };

    The compiler (VC 2005) complains that it can not find definition of func(),
    obvisousely believing that the "extern" refers to a function in global
    namespace.


    I can get it work by declaring it in a header file shared by both files.
    However, I only need this function definition in 2.cpp so that is not what I
    really wanted.
    Maximus, Sep 8, 2008
    #1
    1. Advertising

  2. Maximus

    C C++ C++ Guest

    On Sep 8, 5:02 pm, "Maximus" <> wrote:
    > How to import a function in another translation unit with a namespace? Let's
    > say we have two cpp files:
    >
    > 1.cpp
    >
    > namesapce mu {
    >
    > int func() { return 0;}
    >
    > };
    >
    > and in 2.cpp, I declare
    >
    > namespace mu {
    >
    > extern int func();
    >
    > };
    >
    > The compiler (VC 2005) complains that it can not find definition of func(),
    > obvisousely believing that the "extern" refers to a function in global
    > namespace.
    >
    > I can get it work by declaring it in a header file shared by both files.
    > However, I only need this function definition in 2.cpp so that is not what I
    > really wanted.


    Though i am not very much sure, can you try extern int mu::func();
    C C++ C++, Sep 8, 2008
    #2
    1. Advertising

  3. Maximus wrote:
    > How to import a function in another translation unit with a namespace? Let's
    > say we have two cpp files:
    >
    > 1.cpp
    >
    > namesapce mu {
    >
    > int func() { return 0;}
    >
    > };
    >
    >
    > and in 2.cpp, I declare
    >
    > namespace mu {
    >
    > extern int func();
    >
    > };
    >
    > The compiler (VC 2005) complains that it can not find definition of func(),
    > obvisousely believing that the "extern" refers to a function in global
    > namespace.


    Have you tried removing the "extern" keyword? That's how I always
    declare functions in namespaces.

    (I have to admit I don't know why "extern" would make it not work. I
    always assumed it's kind of implied in all function declarations. I have
    to admit I don't know if things change with namespaces.)
    Juha Nieminen, Sep 8, 2008
    #3
  4. Maximus

    James Kanze Guest

    On Sep 8, 6:02 pm, "Maximus" <> wrote:
    > How to import a function in another translation unit with a
    > namespace? Let's say we have two cpp files:


    > 1.cpp


    > namesapce mu {


    > int func() { return 0;}


    > };


    > and in 2.cpp, I declare


    > namespace mu {


    > extern int func();


    > };


    > The compiler (VC 2005) complains that it can not find
    > definition of func(), obvisousely believing that the "extern"
    > refers to a function in global namespace.


    Obviously not. Post a compilable example of what doesn't work.
    (Both of the files you post compile without problems.)

    > I can get it work by declaring it in a header file shared by
    > both files.


    Which is the correct solution.

    > However, I only need this function definition in 2.cpp so that
    > is not what I really wanted.


    It looks like what you should want to me. You have a function
    defined in one translation unit. You want to use it in another.
    The only acceptable solution is a header file, included in
    *both* translation units.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 9, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    2
    Views:
    3,218
    Andy Fish
    Jul 22, 2005
  2. Scott Allen
    Replies:
    8
    Views:
    10,910
    Scott Allen
    May 2, 2004
  3. Replies:
    0
    Views:
    294
  4. Narendran
    Replies:
    0
    Views:
    447
    Narendran
    Jun 17, 2010
  5. Mellow Crow
    Replies:
    6
    Views:
    412
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page