在我的 dockerfile 中,我有这个:
RUN cmake -j$(nproc) -S . -B build
当我运行它时,它会产生以下错误:
CMake Error: Unknown argument -j16
我有
cmake > 3.12
。这里有什么问题呢?当我在主机的 docker 之外执行此操作时,它运行完美。
如果您想将
-j
选项与 cmake
一起使用,请尝试如下操作:
🗎
Dockerfile
FROM ubuntu:latest
RUN apt-get update -qq && apt-get install -y -qq cmake g++
COPY . .
RUN cmake -S . -B build
RUN cmake --build build -j$(nproc)
宽松地说,第一次运行
cmake
时,它会生成构建目录并写入合适的 Makefile
。第二次使用 cmake
参数运行 --build
时,它实际上执行了构建。它有效地使用 make
命令并接受 -j
参数,指定并发进程的数量。显然,对于下面的示例代码来说,这有点矫枉过正,但对于大型项目来说会产生影响。
完成示例所需的文件:
🗎
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(project)
add_executable(executable main.cpp)
🗎
main.cpp
#include <iostream>
int main() {
std::cout << "Hello, CMake world!" << std::endl;
return 0;
}
构建过程相关部分的日志:
Step 4/5 : RUN cmake -S . -B build
---> Running in 1fceddb78831
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /build
Removing intermediate container 1fceddb78831
---> 73e99ae72e90
Step 5/5 : RUN cmake --build build -j$(nproc)
---> Running in ac07d5ad7e0e
[ 50%] Building CXX object CMakeFiles/executable.dir/main.cpp.o
[100%] Linking CXX executable executable
[100%] Built target executable
Removing intermediate container ac07d5ad7e0e
---> e196a5849f75
检查可执行文件。