2017-01-22 18 views
1

我需要準備SimuroSot(機器人的足球模擬器)戰略(由該程序讀取的dll)。 如果我可以在CLion中編寫它,那將是非常棒的,但我對CMake沒有經驗。如何建立(從DevCpp轉換)使用Cmake SimuroSot模擬器的Win32 DLL

我只有工作版本是DevCpp項目。如何將其轉換爲Cmake?

項目包含:

  • Strategy.h 3個導出的函數和一些結構。
  • Strategy.cpp與DllMain函數

它建立的文件:

  • Team2.dll
  • libTeam2.a
  • libTeam2.def從

IMO重要的選項Strategy.dev:

Compiler=-D__GNUWIN32__ -W -DWIN32 -DNDEBUG -D_WINDOWS -D_MBCS -D_USRDLL [email protected]@_ 
CppCompiler=-D__GNUWIN32__ -W -DWIN32 -DNDEBUG -D_WINDOWS -D_MBCS -D_USRDLL [email protected]@_ 
Linker=-lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lodbc32 [email protected]@_ 

這就是我目前的CMake的,只有Team2.dll創建和導出的函數不執行(可見):

cmake_minimum_required(VERSION 3.6) 
project(SimuroSotTest) 
set(CMAKE_CXX_STANDARD 98) 

set(SOURCE_FILES 
     library.cpp 
     library.h 
     ) 

add_library(Team2 MODULE ${SOURCE_FILES}) 
set_target_properties(Team2 PROPERTIES PREFIX "") 
target_compile_options(Team2 PRIVATE -D__GNUWIN32__ -W -DWIN32 -DNDEBUG -D_WINDOWS -D_MBCS -D_USRDLL -DSTRATEGY_EXPORTS) 
install(TARGETS Team2 DESTINATION /cygdrive/c/Strategy/yellow/) 


# CLion does not suppot above "install" so I have to execute it 
add_custom_target(
     install_all 
     $(MAKE) install 
     DEPENDS Team2 
     COMMENT "Installing ${PROJECT_NAME}" 
) 

library.h:

#ifndef Strategy_H 
#define Strategy_H 

#include <windows.h> 
#include <string.h> 
#include <stdio.h> 

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the STRATEGY_EXPORTS 
// symbol defined on the command line. this symbol should not be defined on any project 
// that uses this DLL. This way any other project whose source files include this file see 
// STRATEGY_API functions as being imported from a DLL, wheras this DLL sees symbols 
// defined with this macro as being exported. 
#ifdef STRATEGY_EXPORTS 
#define STRATEGY_API __declspec(dllexport) 
#else 
#define STRATEGY_API __declspec(dllimport) 
#endif 

typedef struct 
{ 
    double x, y, z; 
} Vector3D; 

typedef struct 
{ 
    long left, right, top, bottom; 
} Bounds; 

typedef struct 
{ 
    Vector3D pos; 
    double rotation; 
    double velocityLeft, velocityRight; 
} Robot; 

typedef struct 
{ 
    Vector3D pos; 
    double rotation; 
} OpponentRobot; 

typedef struct 
{ 
    Vector3D pos; 
} Ball; 

typedef struct 
{ 
    Robot home[5]; 
    OpponentRobot opponent[5]; 
    Ball currentBall, lastBall, predictedBall; 
    Bounds fieldBounds, goalBounds; 
    long gameState; 
    long whosBall; 
    void *userData; 
} Environment; 

/* MUST BE IMPLEMENTED */ 
extern "C" STRATEGY_API void Create (Environment *env); 
extern "C" STRATEGY_API void Strategy (Environment *env); 
extern "C" STRATEGY_API void Destroy (Environment *env); 

#endif // Strategy_H 

library.cpp:

#include "library.h" 

BOOL APIENTRY DllMain(HANDLE hModule, 
DWORD ul_reason_for_call, 
     LPVOID lpReserved 
) 
{ 
switch (ul_reason_for_call) 
{ 
case DLL_PROCESS_ATTACH: 
case DLL_THREAD_ATTACH: 
case DLL_THREAD_DETACH: 
case DLL_PROCESS_DETACH: 
break; 
} 
return TRUE; 
} 

extern "C" STRATEGY_API void Create (Environment *env) 
{ 
    // allocate user data and assign to env->userData 
    // eg. env->userData = (void *) new MyVariables(); 
} 

extern "C" STRATEGY_API void Destroy (Environment *env) 
{ 
    // free any user data created in Create (Environment *) 

    // eg. if (env->userData != NULL) delete (MyVariables *) env->userData; 
} 

extern "C" STRATEGY_API void Strategy (Environment *env) 
{ 
    env->home[0].velocityLeft = 100; 
    env->home[0].velocityRight = 100; 

} 

回答

0

問題是我編譯它爲64位的DLL。

我也改變了Cygwin來MinGW的,所以我的CMakeLists.txt現在看起來如下:

本質是-m32選項(windows風格的路徑是因爲MinGW的的)

cmake_minimum_required(VERSION 3.6) 
project(SimuroSotTest) 
set(CMAKE_CXX_STANDARD 98) 

set(SOURCE_FILES 
     library.cpp 
     library.h 
     ) 

add_library(Team1 MODULE ${SOURCE_FILES}) 
set_target_properties(Team1 PROPERTIES PREFIX "" COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") 
target_compile_options(Team1 PRIVATE -DSTRATEGY_EXPORTS) 
install(TARGETS Team1 DESTINATION C:/Strategy/blue/) 


add_library(Team2 MODULE ${SOURCE_FILES}) 
set_target_properties(Team2 PROPERTIES PREFIX "" COMPILE_FLAGS "-m32" LINK_FLAGS "-m32") 
target_compile_options(Team2 PRIVATE -DSTRATEGY_EXPORTS) 
install(TARGETS Team2 DESTINATION C:/Strategy/yellow/) 


# CLion does not suppot above "install" so I have to execute it 
add_custom_target(
     install_all 
     $(MAKE) install 
     DEPENDS Team1 Team2 
     COMMENT "Installing ${PROJECT_NAME}" 
)