'Compile and Run C++ Application in Alpine Docker Container
I've been provided the source code from an older project in c++ and need to compile and run it in a Docker container running Alpine Linux. I unfortunately don't have experience compiling in Linux or much with c++, with much google, I have tried to deploy a Docker container running Alpine Linux, install compile libraries/tools and try and compile using the make file, but I'm met with compile errors. I can't provide too much details about the source code as its company property, but I can provide the makefile and the steps I've been taking:
install compile tools in Alpine Linux:
apk update && apk add --no-cache autoconf build-base binutils cmake curl file gcc g++ git libgcc libtool linux-headers make musl-dev ninja tar unzip wget
makefile:
BIN := testExec
# relative paths to all project source files
SRCDIR := src
INCDIR := include
IDEDIR := ide_files
# files included in the tarball generated by 'make dist'
DISTFILES := $(BIN).a $(BIN).dll $(SRCDIR) $(INCDIR) $(IDEDIR)
# filename of the tar archive generated by 'make dist'
#DISTOUTPUT := $(BIN).tar.gz
# cross-compiler name prefix
CROSS := i686-pc-mingw32-
#CROSS := /usr/local/gnat-x86_64-darwin/bin/
# toolchain executables
CC := $(CROSS)gcc
CXX := $(CROSS)g++
LD := $(CROSS)gcc
AR := $(CROSS)ar
RM := rm
TAR := tar
PERL := perl
# path to script that generates ctags files
CTAGS := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))ctags.pl
# output files generated by ctags
CTAGS_FILES := .tags .tags_sorted_by_file
# add other macro defines by calling make with a USER_DEFINES argument
# e.g. [ make USER_DEFINES='BLAH FOO="bar"' ] will add the string "-DBLAH -DFOO=bar" to your CFLAGS
UFLAGS := $(addprefix -D,$(USER_DEFINES))
DFLAGS := -DBUILD_DLL
# debugging + major profiling
#DBGFLAGS := -ggdb3 -feliminate-unused-debug-symbols -gno-strict-dwarf -pg -Q -ftime-report -fmem-report
# debugging + minor profiling
#DBGFLAGS := -ggdb3 -feliminate-unused-debug-symbols -gno-strict-dwarf -pg -Q
# debugging
DBGFLAGS := -ggdb3 -feliminate-unused-debug-symbols -gno-strict-dwarf
# ensure the profiling flags get included for the compiler -and- the linker
CFLAGS := $(DBGFLAGS) -Wall $(DFLAGS) $(UFLAGS) $(addprefix -I,$(INCDIR))
LDFLAGS := $(DBGFLAGS) -shared -lm
# flags required for dependency generation; passed to compilers
DEPFLAGS = -MT $@ -MD -MP -MF $(DEPDIR)/$*.Td
COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) -c -o $@
LINK.o = $(LD) $(LDFLAGS) $(LDLIBS) -o [email protected] -Wl,--out-implib,[email protected]
PRECOMPILE =
POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
# source files
SRCS := $(wildcard $(SRCDIR)/*.c)
# intermediate directory for generated object files
OBJDIR := .o
# intermediate directory for generated dependency files
DEPDIR := .d
# object files, auto generated from source files
OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(SRCS)))
# dependency files, auto generated from source files
DEPS := $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))
# compilers (at least gcc and clang) don't create the subdirectories automatically
$(shell mkdir -p $(dir $(OBJS)) >/dev/null)
$(shell mkdir -p $(dir $(DEPS)) >/dev/null)
.PHONY: all
all: $(BIN)
.PHONY: tar
tar: $(DISTFILES)
$(TAR) -czpvf $(DISTOUTPUT) --transform='s|^|$(BIN)/|' $^
.PHONY: dist
dist: $(BIN) tar
.PHONY: clean
clean:
$(RM) -rf $(OBJDIR) $(DEPDIR) $(BIN).a $(BIN).dll
.PHONY: distclean
distclean: clean
$(RM) -f $(DISTOUTPUT)
.PHONY: ctags
ctags:
$(PERL) $(GEN_CTAGS) $(dir $(SRCDIR))
.PHONY: check
check:
@echo no tests configured
.PHONY: help
help:
@echo ""
@echo " available make targets:"
@echo " all - [DEFAULT TARGET] compiles and links all files"
@echo " dist - creates a tarball containing sources and executables"
@echo " clean - removes executables and object file directories"
@echo " distclean - same as clean and also removes dist tarball"
@echo " ctags - creates ctags index files for entire project"
@echo " check - performs verification on project (not implemented)"
@echo ""
@echo " NOTE:"
@echo " you can pass additional flags to the compiler using the USER_DEFINES"
@echo " argument given to make, e.g. [ make USER_DEFINES='BLAH FOO=\"bar\"' ]"
@echo " will add the string \"-DBLAH -DFOO=bar\" to your CFLAGS"
.PHONY: $(BIN)
$(BIN): $(OBJS)
$(LINK.o) $^
$(OBJDIR)/%.o: %.c
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
$(PRECOMPILE)
$(COMPILE.c) $<
$(POSTCOMPILE)
.PRECIOUS = $(DEPDIR)/%.d
$(DEPDIR)/%.d: ;
-include $(DEPS)
The makefile references i686-pc-mingw32-gcc so I've attempted to install the package that matches: https://pkgs.alpinelinux.org/contents?branch=edge&name=mingw-w64-gcc-base&arch=x86&repo=community For some reason I'm not getting the right files with the install though:
apk add mingw-w64-gcc-base --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/
bash-5.1# apk info -L apk add mingw-w64-gcc-base | grep usr/bin
usr/bin/x86_64-w64-mingw32-cpp
usr/bin/x86_64-w64-mingw32-gcc
usr/bin/x86_64-w64-mingw32-gcc-11.2.0
usr/bin/x86_64-w64-mingw32-gcc-ar
usr/bin/x86_64-w64-mingw32-gcc-nm
usr/bin/x86_64-w64-mingw32-gcc-ranlib
usr/bin/x86_64-w64-mingw32-gcov
usr/bin/x86_64-w64-mingw32-gcov-dump
usr/bin/x86_64-w64-mingw32-gcov-tool
usr/bin/x86_64-w64-mingw32-lto-dump
So instead, I tried to install with the x86_64-w64-mingw32-gcc, modified the makefile:
#CROSS := i686-pc-mingw32-
CROSS := x86_64-w64-mingw32-
The source code folder structure is:
/SRC/ide_files
/SRC/include
/SRC/src
the make file is:
/SRC/ide_files/testApp/makefile
I then try:
make -f ide_files/testApp/makefile
It errors with:
x86_64-w64-mingw32-gcc -ggdb3 -feliminate-unused-debug-symbols -gno-strict-dwarf -shared -lm -o testExec.dll -Wl,--out-implib,testExec.a <A lot of .o files>
Error relocating /usr/lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld: qsort_r: symbol not found
collect2: error: ld returned 127 exit status
make: *** [ide_files/testApp/makefile:127: testExec] Error 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
