HiGHS icon indicating copy to clipboard operation
HiGHS copied to clipboard

error: 'strdup' was not declared in this scope, Docker Ubuntu MinGW

Open TAz00 opened this issue 3 years ago • 1 comments

I've been using a docker ubuntu image to build HiGHS using mingw. And it stopped working for the latest HiGHS version

Ubuntu 22.04 only has mingw gcc version 10.3.0

A couple of weeks ago, a single use of strdup was introduced https://github.com/ERGO-Code/HiGHS/commit/646679ff72438434e8f96b599bc8e6f836412543

Error

-- Build files have been written to: /code/HiGHS/build
[  2%] Building CXX object src/CMakeFiles/libhighs.dir/io/FilereaderLp.cpp.o
[  2%] Building CXX object src/CMakeFiles/libhighs.dir/__/extern/filereaderlp/reader.cpp.o
[  2%] Building CXX object src/CMakeFiles/libhighs.dir/io/Filereader.cpp.o
[  3%] Building CXX object src/CMakeFiles/libhighs.dir/io/FilereaderEms.cpp.o
[  3%] Building CXX object src/CMakeFiles/libhighs.dir/io/FilereaderMps.cpp.o
/code/HiGHS/extern/filereaderlp/reader.cpp: In constructor 'ProcessedToken::ProcessedToken(ProcessedTokenType, const string&)':
/code/HiGHS/extern/filereaderlp/reader.cpp:176:14: error: 'strdup' was not declared in this scope
  176 |       name = strdup(s.c_str());
      |              ^~~~~~
make[2]: *** [src/CMakeFiles/libhighs.dir/build.make:76: src/CMakeFiles/libhighs.dir/__/extern/filereaderlp/reader.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/Makefile2:116: src/CMakeFiles/libhighs.dir/all] Error 2
make: *** [Makefile:166: all] Error 2

According to some guy on stackoverflow (in 2016), strdup is not part of the standard c library https://stackoverflow.com/a/40766163

It could very well be that my problem is purely of my own creation, apparently strdup was added into the standard in 2019. https://en.cppreference.com/w/c/experimental/dynamic/strdup and its just a question of waiting for ubuntu to catch up c11.

#dockerfile
FROM ubuntu

RUN apt-get update
RUN apt-get install git -y
RUN apt-get install cmake -y
RUN apt-get install g++-mingw-w64-x86-64 -y				

ENV CROSS_COMPILE x86_64-w64-mingw32-
ENV CC ${CROSS_COMPILE}gcc-posix
ENV CPP ${CROSS_COMPILE}cpp-posix
ENV CXX ${CROSS_COMPILE}g++-posix
ENV LD ${CROSS_COMPILE}ld
ENV AR ${CROSS_COMPILE}ar
ENV RANLIB ${CROSS_COMPILE}ranlib

ENV LD_LIBRARY_PATH "/usr/x86_64-w64-mingw32/bin;/usr/x86_64-w64-mingw32"
ENV CXXFLAGS " -static -static-libgcc -static-libstdc++ -Bstatic -lwinpthread" 

RUN mkdir /code
WORKDIR /code
COPY *.sh /code/
CMD /code/buildscript.sh
#buildscript.sh

echo 'Docker HiGHS builder'
echo 'Target Platform : x86_64-w64-mingw32'
echo 'Current Directory' $(pwd)
echo 'Remove Directory /code/bin'
rm -rf /code/bin 
echo 'Remove Directory /code/HiGHS'
rm -rf /code/HiGHS 
git clone https://github.com/ERGO-Code/HiGHS/
cd HiGHS 
mkdir build
cd build 
cmake -DSHARED=OFF -DCMAKE_BUILD_TYPE=Release -DFAST_BUILD=ON -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=ON /code/HiGHS/ 
make -j4 
mkdir /code/bin
echo 'Copy result to /code/bin'
cp /code/HiGHS/build/bin/highs.exe /code/bin/
rm /code/HiGHS/build/bin/highs.exe

# BuildAndRun.bat
docker build -t highs-mingw32-build:1.0 . 
docker run -v %cd%:/code highs-mingw32-build:1.0

TAz00 avatar Aug 03 '22 12:08 TAz00

That file seems to build natively on MinGW using a build environment from MSYS2: https://github.com/ERGO-Code/HiGHS/runs/7586941356?check_suite_focus=true#step:5:10

Maybe, you'll need to allow ANSI extension with a flag similar to -std=gnu++11, e.g. by setting ENV CXX "${CROSS_COMPILE}g++-posix -std=gnu++11" (and possibly also ENV CPP "${CROSS_COMPILE}cpp-posix -std=gnu++11") in your docker file.

mmuetzel avatar Aug 03 '22 14:08 mmuetzel

So I've tried the -std=gnu++11 approach without any luck. (along with a whole host of variations) -DCMAKE_CXX_FLAGS="-std=gnu++11" -DCMAKE_C_FLAGS="-std=gnu11 -D_GNU_SOURCE"

What I've ended up using so far is the mingw for windows compiler release https://github.com/niXman/mingw-builds-binaries/releases x86_64-12.2.0-release-posix-seh-rt_v10-rev0.7z (and added the /bin dir to my PATH variable)

Installing cmake for windows.

And adding the line :

#include <cstring> to HiGHS\extern\filereaderlp\reader.cpp

And using the following commands to build it cmake -G "MinGW Makefiles" -DCMAKE_CXX_FLAGS=" -static -static-libgcc -static-libstdc++ -Bstatic -lwinpthread -Wall -Wextra -Wno-unused-parameter -Wno-switch -Wno-return-type -Wno-use-after-free -Wno-unused-variable -Wno-sign-compare -Wno-format-truncation -pedantic -O3 -std=gnu++11 -DNDEBUG" -DCMAKE_C_FLAGS="-pedantic -O3 -std=gnu11 -D_GNU_SOURCE" -DCMAKE_CXX_EXTENSIONS=ON -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc -DLIBHIGHS_STATIC_DEFINE=ON -DSHARED=OFF -DCMAKE_BUILD_TYPE=Release -DFAST_BUILD=ON -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=ON .. cmake --build .

Why exactly adding the cstring line works, im not entirely sure. Obviously it couldn't find the definition, the question is why though.

TAz00 avatar Oct 04 '22 11:10 TAz00

@TAz00 Thank you for opening this issue. I also encountered this problem when I attempted to build the current version of HiGHS. If used outside docker, which files or commands should be changed?

I rarely use CMake so I am not familiar with the structure.

Thank you!

smdumlao avatar Oct 05 '22 05:10 smdumlao

I forgot to mention I'm not using docker now. So nothing should need to be changed.

Download x86_64-12.2.0-release-posix-seh-rt_v10-rev0.7z from mingw releases and extract it to C:\temp\build\

Then add C:\temp\build\bin to PATH environment variable

Download and install cmake for windows x64 https://cmake.org/download/

Then add C:\Program Files\CMake\bin to PATH environment variable.

git clone https://github.com/ERGO-Code/HiGHS.git

Add the line : #include <cstring> to HiGHS\extern\filereaderlp\reader.cpp

cd HiGHS
mkdir build
cd build
cmake -G "MinGW Makefiles" -DCMAKE_CXX_FLAGS=" -static -static-libgcc -static-libstdc++ -Bstatic -lwinpthread -Wall -Wextra -Wno-unused-parameter -Wno-switch -Wno-return-type -Wno-use-after-free -Wno-unused-variable -Wno-sign-compare -Wno-format-truncation -pedantic -O3 -std=gnu++11 -DNDEBUG" -DCMAKE_C_FLAGS="-pedantic -O3 -std=gnu11 -D_GNU_SOURCE" -DCMAKE_CXX_EXTENSIONS=ON -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc -DLIBHIGHS_STATIC_DEFINE=ON -DSHARED=OFF -DCMAKE_BUILD_TYPE=Release -DFAST_BUILD=ON -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=ON ..
cmake --build .

It does not have all the features, but it builds a working solver.

TAz00 avatar Oct 05 '22 07:10 TAz00

@TAz00, thank you for sharing your approach. However, I realized that our system is different. You are building on a windows system. I think the cmake code (4th line) will be different.

smdumlao avatar Oct 06 '22 02:10 smdumlao

FYI, just ran into strdup problems when attempting to update SciPy's HiGHS distribution. Would be nice if this was replaced with a more portable function

mckib2 avatar Oct 14 '22 03:10 mckib2

This broke the Julia builds: https://github.com/JuliaPackaging/Yggdrasil/pull/5713

odow avatar Oct 23 '22 22:10 odow

Fixed by #981

jajhall avatar Oct 24 '22 07:10 jajhall