2016-02-23 33 views
-3

我正在使用不是由我創建的庫。代碼用VS2015編譯並按照使用VS2015運行。我想用MinGW GCC編譯器進行編譯,最終使它運行在一臺運行在Linux上並使用GCC編譯器的大型計算機上。該庫應該與VS,MinGW for Windows和GCC for Linux一起工作。 然而,當我嘗試編譯它的代碼:: Blocks的我收到以下錯誤信息:使用VS編譯代碼但不使用MinGW

||=== Build: all in Chrono (compiler: GNU GCC Compiler) ===| C:\Chrono\chrono_source\src\chrono\parallel\ChThreadsSync.h||In member function 'void ChSpinlock::Lock()':| C:\Chrono\chrono_source\src\chrono\parallel\ChThreadsSync.h|81|error: 'YieldProcessor' was not declared in this scope| C:\Chrono\chrono_source\src\chrono\parallel\ChThreadsSync.h|83|error: 'ReadWriteBarrier' was not declared in this scope| C:\Chrono\chrono_source\src\chrono\parallel\ChThreadsSync.h||In member function 'void ChSpinlock::Unlock()':| C:\Chrono\chrono_source\src\chrono\parallel\ChThreadsSync.h|88|error: 'ReadWriteBarrier' was not declared in this scope| src\chrono\CMakeFiles\ChronoEngine.dir\build.make|462|recipe for target 'src/chrono/CMakeFiles/ChronoEngine.dir/physics/ChMarker.cpp.obj' failed| CMakeFiles\Makefile2|1040|recipe for target 'src/chrono/CMakeFiles/ChronoEngine.dir/all' failed| C:\Chrono\Chrono_CodeBlocks\Makefile|159|recipe for target 'all' failed| ||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 4 second(s)) ===|

在錯誤表現在下面列出的部分代碼:

#define EBUSY 16 
#pragma intrinsic(InterlockedExchange) 
#pragma intrinsic(ReadWriteBarrier) 

/// Class that wraps a spinlock, a very fast locking mutex 
/// that should be used only for short wait periods. 
/// This uses MSVC intrinsics to mimic a fast spinlock as 
/// in pthreads.h, but without the need of including 
/// the pthreads library for windows. 
/// See http://locklessinc.com/articles/pthreads_on_windows/ 
class ChApi ChSpinlock { 
    public: 
ChSpinlock() { lock = 0; } 
~ChSpinlock() {} 
void Lock() { 
    while (InterlockedExchange(&lock, EBUSY)) { 
     /* Don't lock the bus whilst waiting */ 
     while (lock) { 
      YieldProcessor(); 
      /* Compiler barrier. Prevent caching of *l */ 
      ReadWriteBarrier(); 
     } 
    } 
} 
void Unlock() { 
    ReadWriteBarrier(); 
    lock = 0; 
} 

    private: 
typedef long pseudo_pthread_spinlock_t; 
pseudo_pthread_spinlock_t lock; 
}; 

所以這是一些據我所知,它使代碼在VisualStudio上運行。 我的問題是,我怎樣才能使用MinGW編譯器進行編譯?

+0

這是「C++」而不是「C」。 'C'沒有課,所以請正確標記你的問題。 –

+0

您不能使用任何未在Linux上的gcc編譯器中出現的僅Microsoft的Visual Studio/VC++功能。你必須堅持使用ANSI C++。 – duffymo

+0

這些都是避免使用'pthreads.h'的解決方法。在Linux上,你可能會希望**使用pthreads。 –

回答

0

它看起來不像庫問題,而是寫在該文件中的代碼。 YOu需要刪除任何MSVC特定的東西,例如#pragma intrinsic()

查看here有人致力於將代碼移植到GCC。

+0

謝謝你的回答。我試圖在代碼中刪除'#pragma intrinsic()'和其他一些東西,但我似乎無法使它工作。我想我會嘗試在Ubuntu上安裝一個VirtualBox,並嘗試使代碼在那裏工作... –

+0

我看了一些ReadWriteBarrier編譯器內在函數(現在不推薦使用)的替代品。您可能希望切換到使用來處理需要跨線程同步訪問的數據。 –