[英]C++: Switching from MSVC to G++: Global Variables
我最近改用Linux,並希望在G ++上編譯僅使用STL的Visual Studio 2010 C ++源代碼。
我的Linux機器目前不可用,但我可以先告訴您發生了什么事:
main
使用的所有全局變量以及在MSVC上myGlobalVar is not defined in this scope
正常運行的所有全局變量都導致myGlobalVar is not defined in this scope
錯誤中myGlobalVar is not defined in this scope
。 我的項目的構建幾乎與以下示例相同:
// 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
}
您需要進行如下編譯:
g ++ main.cpp myclass.cpp -o myapp
不如下:g ++ main.cpp -o myapp,它將丟失myclass.cpp文件中的全局變量聲明。
您的示例代碼在Linux和Windows上都應該可以正常工作。 就全局變量的可見性而言,GCC和MSVC之間不應有任何區別。 我認為您所看到的更有可能是另一個問題的征兆。
我唯一想到的可能會引起類似問題的東西就是“擰緊”頭文件,並使用技術術語。 從Windows移植代碼到Linux的一個常見問題是頭文件區分大小寫。 盡管MSVC不在乎是否將MyHeader.h導入為#include <myheader.h>
但在Linux上肯定會失敗。 如果不包含標頭,則編譯器將錯過extern聲明,並可能導致您看到的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.