import an external function

M

Maximus

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.
 
C

C C++ C++

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();
 
J

Juha Nieminen

Maximus said:
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.)
 
J

James Kanze

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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top