views:

229

answers:

2

I recently switched to Linux and wanted to compile my Visual Studio 2010 C++ source code, which uses only the STL, on G++.

My Linux machine currently isn't available but I can try to tell you what is going on, first:

  • As I try to compile my project, all global variables I use in main and which perfectly work on MSVC result in myGlobalVar is not defined in this scope errors.

My project is built nearly the same as the example below:

// myclass.h
class myClass
{
 // ....
};
extern myClass globalInstance;

// myclass.cpp
#include "myclass.h"
// myClass functions located here
myClass globalInstance;

// main.cpp
#include "myclass.h"
int main( )
{
    // Accessing globalInstance results in an error: Not defined in this scope
}
  • What am I doing wrong?
  • Where are the differences between G++ and MSVC in terms of global variables?
+1  A: 

Your sample code should work just fine on Linux as well as Windows. There shouldn't be any differences between GCC & MSVC with regards to visibility of global variables. I think it's more likely that what you're seeing is a symptom of another problem.

The only thing I can think off off the top of my head that might cause an issue like this would be "screwed up" header files, to use the technical term for it. A common issue in porting code from Windows to Linux is header file case sensitivity. Whereas MSVC won't care if you import MyHeader.h as #include <myheader.h> it will certainly fail on Linux. If you header isn't being included, the compiler would miss the extern declaration and might result in the error you're seeing.

Rakis
Based on the comments above, it's probably not the specific header file issue I was thinking of.
Rakis
+2  A: 

you need to compile as follow:

g++ main.cpp myclass.cpp -o myapp

NOT as follow: g++ main.cpp -o myapp which will miss global variable declaration in myclass.cpp file.

Huh? I thought the global variable declarations were in myclass.h, not myclass.cpp. The file myclass.cpp is irrelevant surely? Why would it appear on the compile line at all?
Troubadour
or learn how to use make, cmake, autotools, or...
Noah Roberts
If that were Troubadour's problem, the error would come from the linker about a missing symbol, not from the compiler about an undefined variable.
Dennis Zickefoose
@Dennis: I think you mean "feed the fire" instead of me? I agree that this answer fixes a linking error and not the compilation error that the OP posted ("...in this scope").
Troubadour
@feed the fire: Note that it would probably be more standard to add myclass.o to the build line for main.cpp rather than myclass.cpp. The object file myclass.o would be built from a separate compile line such as `g++ -c myclass.cpp`. Doing it the way @myona suggests means there is no intermediate object file and as your project grows bigger you will be hit with longer and longer compilation times. As @Noah suggests familiarise yourself with `make`, or a wrapper for it, so that you only need to rebuild the things that have changed.
Troubadour