File scope.

Hello,

After some very odd "unresolved extern symbol" errors, I'm wondering whether there's such a thing as "file scope".
Basically, I have one header file with a class definition, that holds the declaration for nearly all functions in the project. To split up the code load, I've subgrouped the functions by "module". Now, it turns out a function in one .cpp file can't call a function in a different .cpp file (even if they both belong to the same class and are thus declared in the same header file).

Is there a way around this?
You need to link the objects.
¿What environment are you using?

$ g++ -c *.cpp #compile
$ g++ *.o -l{some,other,libraries} -o program.bin #linking
Using MS Visual C++ Express 2010. I'll try looking around!
Make sure you're adding all the source files to your project/solution.
They're all in the project. The problem only occurs when function A uses function B, but function B's definition is in a different file.

It feels like one file (with function A) is being compiled before the other (with function B), thus the compiler doesn't know what to fill in for the call to B. My instincts tell me that's not how compilers work though.

[edit]

It's not a game-breaking problem; I'll just spend a bit more time deciding which code goes where. Would still be handy to know if this can be avoided.
Last edited on
My instincts tell me that's not how compilers work though.
Correct. To compile a file you only need the function declarations before you use them.

Don't know about your IDE. But check what it is doing (how is compiling, what files, etc)
Maybe you would have more control generating a makefile.
My guess is that the function in question is an inline function with external linkage - this would need to be defined (with the same definition) in every translation unit in which it is used.

If there are inline functions, move their definitions to the header file.
Ah yes, it was declared "inline", but the actual body of the function was somewhere else. Makes perfect sense now that I think about it. Thanks, JLBorges!

@ne555: I've never used makefiles (or the command line, or anything beyond the IDE). I'm not sure they'd be very useful for my line of work, but if you know of any easy (and preferably short) introduction on makefiles, I'd love a link!
Topic archived. No new replies allowed.