使用CMake构建Windows DllMain DLL [英] Build Windows DllMain DLL with CMake

查看:213
本文介绍了使用CMake构建Windows DllMain DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于这个问题的问题。我正在尝试为Windows编译DLL,类似于使用Visual Studio的Windows,但使用CLion和CMake除外。我已经尝试了问题的答案,以及这里所示的步骤,但是我仍然可以

I have a question similar to this one. I am trying to compile a DLL for windows similar to how Visual Studio would, except with CLion and CMake. I've tried the answer in the question, as well as the steps shown here, but I still get an error while injecting.

我的dll代码非常简单,在Visual Studio中编译的类似dll可以正常工作:

My dll code is very simple, a similar dll compiled in visual studio works fine:

#include <windows.h>
#include <iostream>
using namespace std;

void hello() {
    AllocConsole();
    freopen("CONOUT$", "w", stdout);
    cout << "Hello, World!" << endl;
}

bool __stdcall DllMain(HMODULE /*module*/, DWORD reason, LPVOID /*reserved*/) {
    if (reason == DLL_PROCESS_ATTACH) hello();
    return true;
}

此外,这也是我在 CMakeLists.txt中尝试过的内容对不起,PROJECT_NAME和MODULE之间应该有一个空格

cmake_minimum_required(VERSION 3.9)
project(PROJECT_NAME)
include (GenerateExportHeader)
set(CMAKE_CXX_STANDARD 17)

add_library(PROJECT_NAME MODULE main.cpp)
set_target_properties(PROJECT_NAME PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
GENERATE_EXPORT_HEADER(PROJECT_NAME
    BASE_NAME PROJECT_NAME
    EXPORT_MACRO_NAME PROJECT_NAME_EXPORT
    EXPORT_FILE_NAME PROJECT_NAME_Export.h
    STATIC_DEFINE PROJECT_NAME_BUILT_AS_STATIC)


推荐答案

您有两个选择:


  1. BUILD_SHARED_LIBS 变量作为布尔值添加到CMake的缓存中,然后进行检查。这将修改 add_library 命令的行为,以创建共享库,即Windows上的DLL文件。

  2. 将库创建为共享的: add_library(PROJECT_NAMEMODULE SHARED main.cpp)

  1. Add the BUILD_SHARED_LIBS variable to CMake's cache as a boolean value then check it. This will modify the behaviour of the add_library command to make a shared library i.e. a DLL file on Windows.
  2. Explicitly create the library as shared: add_library(PROJECT_NAMEMODULE SHARED main.cpp)

BUILD_SHARED_LIBS 变量文档: https:/ /cmake.org/cmake/help/v3.10/variable/BUILD_SHARED_LIBS.html

这篇关于使用CMake构建Windows DllMain DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆