简体   繁体   中英

How to build a cross-platform C++ library in Windows and Linux

I am a Linux user and a beginner in C++. I am developing a small library with the following structure

  • src
    • main.cpp
    • makefile
  • include
    • Inputs.h
    • GenerateTabValues.h
    • Prototypes.h
  • data
    • TabNodes.csv
    • TabWeights.csv
  • output
    • test1
      • Results.txt
    • test2 ...

In Linux I usually compile C++ using gcc and, when I have multiple dependencies, I use make with a makefile. Below the one for the current case

CXX = g++

CXXFLAGS = -g -Wall
DLIBS = -lm -lgsl -lgslcblas -lgmp -lquadmath

DEPS = ../../include
TEST1 = ../../output/test1
TEST2 = ../../output/test2
TEST3 = ../../output/test3

vpath %.h $(DEPS)

main: *.cpp $(DEPS)/*.h $(TEST1)/*.h
    g++ $(CXXFLAGS) -I $(DEPS) -I $(TEST1) *.cpp -o main $(DLIBS)

all: test1 test2 test3

test1: *.cpp $(DEPS)/*.h $(TEST1)/*.h
    g++ $(CXXFLAGS) -I $(DEPS) -I $(TEST1) *.cpp -o main $(DLIBS)
    ./main test1

test2: *.cpp $(DEPS)/*.h $(TEST2)/*.h
    g++ $(CXXFLAGS) -I $(DEPS) -I $(TEST2) *.cpp -o main $(DLIBS)
    ./main test2

test3: *.cpp $(DEPS)/*.h $(TEST3)/*.h
    g++ $(CXXFLAGS) -I $(DEPS) -I $(TEST3) *.cpp -o main $(DLIBS)
    ./main test3

clean:
    rm -f main 

I must now make my library cross-platform and able to compile and run on Windows.

Ideally I would like to retain the same structure and perhaps add a build folder containing linux and win subfolders with the former having the aforementioned makefile. However I am not sure on how to compile it on Windows.

Is there an equivalent to make in Windows? Should I use MSVC with MinGW? Any help or suggestion on which might be the best approach for flexible portability and cross-compilation?

Use either MSVC or MinGW-w64, and make sure not to mix them.

But if you come from a Linux world you should stick with MinGW-w64 and maybe even consider MSYS2 which gives you a bash shell.

Check out my a minimal example for a cross-platform library here: https://github.com/brechtsanders/ci-test

That project has both Makefile (not for MSVC) and CMakeLists.txt which work across platforms (not MSVC though).

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