libmodbus icon indicating copy to clipboard operation
libmodbus copied to clipboard

build lib and dll with vs2015 under win64

Open YiGene opened this issue 5 years ago • 15 comments

it take me some time to get it build to 64bit under 2015 hence i put a note here

after we choose x64 to debug and release, run build image

we see below errors: 1> d:\modbus\libmodbus-3.1.6\src\modbus.h(228): note: see previous definition of 'modbus_mapping_free' 1> Generating Code... 1> Link: 1> Creating library D:\modbus\libmodbus-3.1.6\src\win32\x64\Debug\modbus.lib and object D:\modbus\libmodbus-3.1.6\src\win32\x64\Debug\modbus.exp 1> 1> 1>modbus-rtu.obj : warning LNK4217: locally defined symbol modbus_free imported in function modbus_new_rtu 1> 1> 1>modbus-tcp.obj : warning LNK4217: locally defined symbol modbus_free imported in function _modbus_tcp_init_win32 1> 1> 1>modbus.obj : warning LNK4217: locally defined symbol modbus_set_bits_from_bytes imported in function modbus_reply 1> 1> 1>modbus-data.obj : error LNK2019: unresolved external symbol __imp_htonl referenced in function modbus_set_float_abcd 1> 1>

add ws2_32.lib image

build success

modbus.obj : warning LNK4217: locally defined symbol modbus_set_bits_from_bytes imported in function modbus_reply 1> modbus.vcxproj -> D:\modbus\libmodbus-3.1.6\src\win32\x64\Debug\modbus.dll 1> modbus.vcxproj -> D:\modbus\libmodbus-3.1.6\src\win32\x64\Debug\modbus.pdb (Full PDB) 1> FinalizeBuildStatus: 1> Deleting file "x64\Debug\modbus.tlog\unsuccessfulbuild". 1> Touching "x64\Debug\modbus.tlog\modbus.lastbuildstate". 1> 1>Build succeeded. 1> 1>Time Elapsed 00:00:12.33 ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

**PLEASE NOTE i didn't test the function, just build it **

YiGene avatar Aug 26 '20 07:08 YiGene

Hi YiGene, Related to this I wrote this simple CMakelists.txt in order to create Visual Studio solutions using CMake tool and it seems it can also be compiled and it works ok, just if it is of interest.

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

#include("./TargetArch.cmake")
#target_architecture (TARGET_COMPILATION_ARCH)
#check : IF(TARGET_COMPILATION_ARCH MATCHES x86_64)
#message(STATUS "** Compilation Target is ${TARGET_COMPILATION_ARCH} bits **")

PROJECT (modbus)

#Version
SET( modbus_VERSION_MAJOR 1 )
SET( modbus_VERSION_MINOR 1 )
SET( modbus_VERSION_PATCH 0 )
SET( modbus_VERSION_STATUS "dev") 
SET( modbus_PROJECT_VERSION ${modbus_VERSION_MAJOR}.${modbus_VERSION_MINOR}.${modbus_VERSION_PATCH}-${modbus_VERSION_STATUS})

#Set folders in Visual studio
set_property( GLOBAL PROPERTY USE_FOLDERS On)

#Source files
FILE (GLOB sources "src/*.h" "src/*.c")

#Create config.h from config.h.win32
CONFIGURE_FILE("${PROJECT_SOURCE_DIR}/src/config.h.win32" "${PROJECT_BINARY_DIR}/config.h" @ONLY)
#Include the directory containing config.h in include directories list
include_directories(${PROJECT_BINARY_DIR})

#add target 
add_library(modbus SHARED ${sources} "${PROJECT_BINARY_DIR}/config.h")

#add ws2_32 library
target_link_libraries(modbus ws2_32 )

#add preprocessor directives 
target_compile_definitions( modbus PUBLIC  "-DDLLBUILD")
target_compile_definitions( modbus PUBLIC  "MODBUS_VERSION=${modbus_PROJECT_VERSION}")
CONFIGURE_FILE("${PROJECT_SOURCE_DIR}/config.cmake.in" "${PROJECT_BINARY_DIR}/modbusConfig.cmake" @ONLY)

config.cmake.in is the file used to create modbusConfig.cmake (used by FindPackage to generate configuration variables). Here the content:

#This file will define the following variables:
#    - modbus_LIBRARIES        : The list of libraries to links against.
#    - modbus_INCLUDE_DIRS  : The directory where include files are.
 
#    - modbus_VERSION       : The  version of this build. Example: "1.2.0"
#    - modbus_VERSION_MAJOR : Major version part of VERSION. Example: "1"
#    - modbus_VERSION_MINOR : Minor version part of VERSION. Example: "2"
#    - modbus_VERSION_PATCH : Patch version part of VERSION. Example: "0"

set (modbus_INCLUDE_DIRS "@PROJECT_BINARY_DIR@" "@PROJECT_SOURCE_DIR@/src")
set (modbus_LIB_DIR  "@PROJECT_BINARY_DIR@/$(Configuration)")
set(modbus_LIBRARIES debug @PROJECT_BINARY_DIR@/Debug/modbusd.lib optimized @PROJECT_BINARY_DIR@/Release/modbus.lib)
set(modbus_VERSION 		  @modbus_VERSION@)
set(modbus_VERSION_MAJOR  @modbus_VERSION_MAJOR@)
set(modbus_VERSION_MINOR  @modbus_VERSION_MINOR@)
set(modbus_VERSION_PATCH  @modbus_VERSION_PATCH@)

#Usage from an external project:
#    In your CMakeLists.txt, add these lines:
#         FIND_PACKAGE(modbus REQUIRED )
#         INCLUDE_DIRECTORIES(${modbus_INCLUDE_DIRS})
#         TARGET_LINK_LIBRARIES(TARGET_NAME ${modbus_LIBRARIES})

AndoniC avatar Oct 21 '20 10:10 AndoniC

Related to 9a7ea129283a8a69de0f36572ad2f9261d0003ac

stephane avatar Nov 28 '22 00:11 stephane

Hi @stephane and everyone. I have compiled and used this library in both Windows and Linux. I found that the README file for Windows (inside src/win32 folder) is not complete as it does not tell the user how to create a dll out of this. For creating dll I had to add "__declspec(dllexport)" to all the functions that I used in my code.

I am not sure if you consider this useful but if you do then I am ready to volunteer to make the README file bigger with steps to create a dll file.

Just to elaborate I added a Macros DLL_EXPORT in modbus.h file as below:

#ifdef _WIN32
    #define DLL_EXPORT __declspec(dllexport)
#endif

And then in appropriate header file I added this DLL_EXPORT to all the important methods that should be exported. One Example is shown :

MODBUS_API modbus_t * DLL_EXPORT
modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit);

Let me know what you think of this making README more elaborative!

msk-repo-0x4d avatar Apr 15 '23 09:04 msk-repo-0x4d

Yes please, this would be very useful information.

stephane avatar Apr 17 '23 21:04 stephane

@stephane I just created a pull request (https://github.com/stephane/libmodbus/pull/697) after making changes. Let me know if there is something I have to explain better. Also, I left the Visual Studio part as it is, as I can't test it on my system (my system is running low on storage space).

msk-repo-0x4d avatar Apr 19 '23 11:04 msk-repo-0x4d

Hi @stephane. Did you get a chance to look at the new README for Windows (under src/win32)?

msk-repo-0x4d avatar May 03 '23 06:05 msk-repo-0x4d

Why no one can leave the build dll files here so that we can download them? Rights problems? Can anyone please upload it? THX

ottelo9 avatar May 12 '23 09:05 ottelo9

@ottelo9 Check the wiki page of this project. They have mentioned a google-drive link to share it. Navigate here in case you are having difficulty: https://github.com/stephane/libmodbus/wiki/Compile-dll-under-Windows-with-Visual-Studio-2008-Express#ready-to-use-dll

msk-repo-0x4d avatar May 16 '23 17:05 msk-repo-0x4d

thanks for your answer. That is my dll that I uploaded few years ago 😂😂😂. Yesterday I compiled v3.1.10. I will upload it again and put it on wiki.

ottelo9 avatar May 17 '23 06:05 ottelo9

thanks for your answer. That is my dll that I uploaded few years ago 😂😂😂. Yesterday I compiled v3.1.10. I will upload it again and put it on wiki.

It seems that the 64-bit version cannot be compiled. I can compile the 32-bit version. Can you compile the 64-bit version of the DLL?

luocheng610 avatar Jul 13 '23 02:07 luocheng610

@luocheng610 I use Visual Studio 2008 Express. I can only compile win32 versions. I dont know how to compile 64Bit :( . But please! If anyone can share a 64Bit libmodbus, please do it.

ottelo9 avatar Sep 19 '23 09:09 ottelo9

@luocheng610 I use Visual Studio 2008 Express. I can only compile win32 versions. I dont know how to compile 64Bit :( . But please! If anyone can share a 64Bit libmodbus, please do it.

No problem, I'll upload it libmodbus-3.1.10_VS2008_X64.zip

luocheng610 avatar Sep 20 '23 09:09 luocheng610

@luocheng610 Thanks. I put it to the Wiki.

ottelo9 avatar Sep 20 '23 16:09 ottelo9