我需要准备 SimuroSot(机器人的足球模拟器)策略(此程序读取的 dll)。 如果我能用 CLion 编写它会很棒,但我没有使用 CMake 的经验。
我唯一的工作版本是 DevCpp 项目。如何将其转换为 Cmake?
项目包含:
- 具有 3 个导出函数和一些结构的 Strategy.h。
- 带 DllMain 函数的 Strategy.cpp
它构建文件:
- Team2.dll
- libTeam2.a
- libTeam2.def
来自 Strategy.dev 的 IMO 重要选项:
Compiler=-D__GNUWIN32__ -W -DWIN32 -DNDEBUG -D_WINDOWS -D_MBCS -D_USRDLL -DSTRATEGY_EXPORTS_@@_
CppCompiler=-D__GNUWIN32__ -W -DWIN32 -DNDEBUG -D_WINDOWS -D_MBCS -D_USRDLL -DSTRATEGY_EXPORTS_@@_
Linker=-lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lodbc32 -lodbccp32_@@_
这是我当前的 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}"
)
库.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
库.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;
}
最佳答案
问题是我将它编译为 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}"
)
关于c++ - 如何使用 Cmake 为 SimuroSot 模拟器构建(从 DevCpp 转换)Win32 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41785921/