首页 > 解决方案 > Docker/C++ 问题 - 编译错误 /usr/bin/ld: cannot open output file server: Is a directory

问题描述

我正在尝试在 Docker 中容器化的 ubuntu 中使用 cmake 编译 C++ 程序。如果没有 Docker,我可以让它工作得很好,但是有了它,我似乎遇到了一些错误,无论我似乎无法修复它们:/

我试图解决改变许多不同组合的路径,希望我只是写在错误的路径上。

FROM ubuntu:16.04

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in the requirements.txt
RUN apt-get update && apt-get -y install g++ make cmake-curses-gui libsqlite3-dev libmariadb-client-lgpl-dev subversion

# Make port 8078 available to the world outside this container
EXPOSE 8078

# Retrieve EOServ and build it
RUN svn checkout svn://eoserv.net/eoserv/trunk/ /app/eoserv
RUN cd /app/eoserv && mkdir build && cd build
RUN cmake -G "Unix Makefiles" /app/eoserv
RUN make

# Run ./eoserv when the container launches
RUN /app/eoserv/eoserv
# Here I've tried several options like
# RUN ./eoserv
# RUN cd /app/eoserv && ./eoserv

预期的结果将是所需文件夹中的 eoserv 二进制文件,当我不在 docker 映像中运行它时,它可以工作,而是在没有 Docker 的情况下自行创建。实际结果是:

[ 91%] Building C object CMakeFiles/eoserv.dir/tu/sha256.c.o
[100%] Linking CXX executable eoserv
/usr/bin/ld: cannot open output file eoserv: Is a directory
collect2: error: ld returned 1 exit status
CMakeFiles/eoserv.dir/build.make:305: recipe for target 'eoserv' failed
make[2]: *** [eoserv] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/eoserv.dir/all' failed
make[1]: *** [CMakeFiles/eoserv.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
The command '/bin/sh -c make' returned a non-zero code: 2

标签: c++dockermakefile

解决方案


RUN指令启动一个新的 shell。因此,您RUN之前的命令将仅是该 shell 的本地命令,其中包括诸如 之类的内容cd,并且下一RUN条指令将在不了解前一个的情况下启动一个新的 shell。

说明

RUN cd /app/eoserv && mkdir build && cd build
RUN cmake -G "Unix Makefiles" /app/eoserv
RUN make

需要组合成一条 RUN指令

RUN cd /app/eoserv && mkdir build && cd build && cmake -G "Unix Makefiles" /app/eoserv && make

您当然可以编写一个运行命令的脚本,并使用RUN指令调用该脚本。


推荐阅读