Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile with current version of MinGW64 and add _declspec(dllexport/import) #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions libserialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,32 @@ extern "C" {

/** @cond */
#ifdef _MSC_VER
/* Microsoft Visual C/C++ compiler in use */
#ifdef LIBSERIALPORT_MSBUILD
/* Building the library - need to export DLL symbols */
#define SP_API __declspec(dllexport)
/* Microsoft Visual C/C++ compiler in use */
#ifdef LIBSERIALPORT_MSBUILD
// Building the library - need to export DLL symbols
#define SP_API __declspec(dllexport)
#else
// Using the library - need to import DLL symbols
#define SP_API __declspec(dllimport)
#endif
// #define SP_CALL
#elif defined(__MINGW32__)
// You should define LIBSERIALPORT_MINGW64BUILD *only* when building the DLL.
#ifdef LIBSERIALPORT_MINGW64BUILD
#define SP_API __declspec(dllexport)
#else
#define SP_API __declspec(dllimport)
#endif
// Define calling convention in one place, for convenience.
// On MinGW64 "__cdecl" is default value.
// #define SP_CALL __cdecl
#else
/* Using the library - need to import DLL symbols */
#define SP_API __declspec(dllimport)
#endif
#else
/* Some other compiler in use */
#ifndef LIBSERIALPORT_ATBUILD
/* Not building the library itself - don't need any special prefixes. */
#define SP_API
#endif
/* Some other compiler in use */
#ifndef LIBSERIALPORT_ATBUILD
/* Not building the library itself - don't need any special prefixes. */
#define SP_API
#endif
// #define SP_CALL
#endif
/** @endcond */

Expand Down
10 changes: 10 additions & 0 deletions libserialport_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
#define SP_PRIV
#endif

#ifdef LIBSERIALPORT_MINGW64BUILD
/* If building with MinGW64 tools, define necessary things that
would otherwise appear in config.h. */
#define SP_PRIV
#endif

#ifndef SP_PRIV
#error "You should define one of LIBSERIALPORT_ATBUILD, LIBSERIALPORT_MSBUILD, LIBSERIALPORT_MINGW64BUILD"
#endif

#include "libserialport.h"

#include <string.h>
Expand Down
53 changes: 53 additions & 0 deletions mingw64.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# mingw64.mak: build libserialport.dll using MinGW64-w64

# Program for compiling C programs
CC = gcc

# Extra flags to give to the C preprocessor
CPPFLAGS =
CPPFLAGS += -DLIBSERIALPORT_MINGW64BUILD
# CPPFLAGS += -DLIBSERIALPORT_MSBUILD
# CPPFLAGS += -DLIBSERIALPORT_ATBUILD
# CPPFLAGS += -DHAVE_CONFIG_H

# Extra flags to give to the C compiler.
CFLAGS =
CFLAGS += -I. -std=c99
CFLAGS += -Wall -Wextra -pedantic -Wmissing-prototypes -Wshadow
CFLAGS += -g -O2

# Extra flags when invoking the linker
LDFLAGS = -s -shared -Wl,--subsystem,windows

# Library flags or names when invoking the linker
LDLIBS = -lsetupapi

# Command to remove a file
RM = rm -f

H_FILES = libserialport_internal.h libserialport.h
O_FILES = serialport.o timing.o windows.o

all: libserialport.dll

libserialport.dll: $(O_FILES)
gcc -o libserialport.dll $(O_FILES) $(LDFLAGS) $(LDLIBS)

serialport.o: serialport.c $(H_FILES)
$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<

timing.o: timing.c $(H_FILES)
$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<

windows.o: windows.c $(H_FILES)
$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<

install:
@echo "install is not yet implemented"
exit 1

clean:
$(RM) libserialport.dll
$(RM) *.o

.PHONY: all install clean