简体   繁体   中英

How do I call functions in an .exe from a .dll

I have a main executable that dynamically loads a shared library, both are compiled in the same CMake file. The library calls a function defined in the main executable, in Linux this is done successfully and the program works as expected, however in Windows the library fails to link the executable and the program crashes during compilation.

I am using this cmake file to construct an executable and a library that I modified from the Unix version I found here .

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(EXPORTS C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c
"#include <windows.h>
#include <stdio.h>
void internal(void){printf(\"internal()\\n\");}
int main(void)
{
    HMODULE plugin_library = LoadLibrary (\"./plugin.dll\");
    FARPROC initizer = GetProcAddress(plugin_library, \"external\");
    initizer();
}\n")
ADD_EXECUTABLE(main main.c)
target_link_libraries(main ${CMAKE_DL_LIBS})
SET_TARGET_PROPERTIES(main PROPERTIES
    ENABLE_EXPORTS TRUE)
FILE(WRITE ${CMAKE_BINARY_DIR}/plugin.c
"void external(void){internal();}\n")
ADD_LIBRARY(plugin MODULE plugin.c)
TARGET_LINK_LIBRARIES(plugin main)

When I try to build the program I get this output which throws the error fatal error U1073: don't know how to make 'main.lib' .

C:\Users\User\Desktop\linkingdemo\build>nmake

Microsoft (R) Program Maintenance Utility Version 14.00.23026.0
Copyright (C) Microsoft Corporation.  All rights reserved.

        "C:\Program Files\CMake\bin\cmake.exe" -HC:\Users\User\Desktop\linkingdemo -BC:\Users\User\Desktop\linkingdemo\build --check-build-system CMakeFiles\Makefile.cmake 0
        "C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start C:\Users\User\Desktop\linkingdemo\build\CMakeFiles C:\Users\User\Desktop\linkingdemo\build\CMakeFiles\progress.marks
        "C:\Program Files\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\Makefile2 /nologo -                   all
        "C:\Program Files\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\main.dir\build.make /nologo -L                  CMakeFiles\main.dir\depend
        "C:\Program Files\CMake\bin\cmake.exe" -E cmake_depends "NMake Makefiles" C:\Users\User\Desktop\linkingdemo C:\Users\User\Desktop\linkingdemo C:\Users\User\Desktop\linkingdemo\build C:\Users\User\Desktop\linkingdemo\build C:\Users\User\Desktop\linkingdemo\build\CMakeFiles\main.dir\DependInfo.cmake --color=
Scanning dependencies of target main
        "C:\Program Files\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\main.dir\build.make /nologo -L                  CMakeFiles\main.dir\build
        "C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_report C:\Users\User\Desktop\linkingdemo\build\CMakeFiles 1
[ 50%] Building C object CMakeFiles/main.dir/main.c.obj
        C:\PROGRA~1\MICROS~1.0\VC\bin\cl.exe  @C:\Users\User\AppData\Local\Temp\nm3E0E.tmp
main.c
Linking C executable main.exe
        "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe C:\PROGRA~1\MICROS~1.0\VC\bin\link.exe /nologo @CMakeFiles\main.dir\objects1.rsp @C:\Users\User\AppData\Local\Temp\nm3FE4.tmp
        "C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_report C:\Users\User\Desktop\linkingdemo\build\CMakeFiles  1
[ 50%] Built target main
        "C:\Program Files\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\plugin.dir\build.make /nologo -L                  CMakeFiles\plugin.dir\depend
        "C:\Program Files\CMake\bin\cmake.exe" -E cmake_depends "NMake Makefiles" C:\Users\User\Desktop\linkingdemo C:\Users\User\Desktop\linkingdemo C:\Users\User\Desktop\linkingdemo\build C:\Users\User\Desktop\linkingdemo\build C:\Users\User\Desktop\linkingdemo\build\CMakeFiles\plugin.dir\DependInfo.cmake --color=
Scanning dependencies of target plugin
        "C:\Program Files\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\plugin.dir\build.make /nologo -L                  CMakeFiles\plugin.dir\build
        "C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_report C:\Users\User\Desktop\linkingdemo\build\CMakeFiles 2
[100%] Building C object CMakeFiles/plugin.dir/plugin.c.obj
        C:\PROGRA~1\MICROS~1.0\VC\bin\cl.exe  @C:\Users\User\AppData\Local\Temp\nm43BB.tmp
plugin.c
C:\Users\User\Desktop\linkingdemo\build\plugin.c(1): warning C4013: 'internal' undefined; assuming extern returning int
NMAKE : fatal error U1073: don't know how to make 'main.lib'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.

How do I get to get this program to work correctly on Windows?

It appears in Windows you must specify __declspec(dllexport) on all of the functions that can be accessed by a remote library. I have added it to each of the functions that need to be run by an external library and the program now works correctly.

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(EXPORTS C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c
"#include <windows.h>
#include <stdio.h>
__declspec(dllexport) void internal(void){printf(\"internal()\\n\");}
int main(void)
{
    HMODULE plugin_library = LoadLibrary (\"./plugin.dll\");
    FARPROC initizer = GetProcAddress(plugin_library, \"external\");
    initizer();
}\n")
ADD_EXECUTABLE(main main.c)
target_link_libraries(main ${CMAKE_DL_LIBS})
SET_TARGET_PROPERTIES(main PROPERTIES
    ENABLE_EXPORTS TRUE)
FILE(WRITE ${CMAKE_BINARY_DIR}/plugin.c
"__declspec(dllexport) void external(void){internal();}\n")
ADD_LIBRARY(plugin MODULE plugin.c)
TARGET_LINK_LIBRARIES(plugin main)

This program was built and run using the following commands using Visual Studio 2015

cmake -G "NMake Makefiles" ..
nmake
main.exe

and printed

internal()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM