Skip to content

Instantly share code, notes, and snippets.

@RedactedProfile
Created February 2, 2024 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedactedProfile/a74eafa66efbd8129d1872bdd6d20853 to your computer and use it in GitHub Desktop.
Save RedactedProfile/a74eafa66efbd8129d1872bdd6d20853 to your computer and use it in GitHub Desktop.
Minimal C++20 with Module support for Linux, using Docker DevContainer
.git
!.gitkeep
.vs
.clion
.idea
bin
obj
Makefile
*.make
/llvm.sh
/.env
/cmake-build-debug
/cmake-build-release
work/**/*
!work/.gitkeep
*.workspace
*.layout
*.cbp
*.depend
CMakeLists.txt
*.cmake
.cache
.gnupg
.lldb
.local
.run
.vscode
.vscode-server
build
/CMakeFiles
/include
!.gitkeep
.vs
.vscode
.clion
.idea
.codelite
.cache
.ctagsd
bin
build
obj
Makefile
*.make
/llvm.sh
/.env
/cmake-build-debug
/cmake-build-release
work/**/*
!work/.gitkeep
*.workspace
*.layout
*.cbp
*.depend
# CMakeLists.txt
# *.cmake
CMakeFiles
CMakeCache.txt
docproc2.*
./include/*
.vscode-server
.local
.bash_history
cmake_install.cmake
cmake_minimum_required(VERSION 3.28)
project(myproject)
# Set the C++ standard to C++20
set(${PROJECT_NAME} CMAKE_CXX_STANDARD 20)
set(${PROJECT_NAME} CMAKE_CXX_STANDARD_REQUIRED True)
# Set the compiler to Clang (if necessary)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
# Enable C++20 Module Support for Clang
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -fmodules -fcxx-modules")
# Find required libraries
find_package(ZLIB REQUIRED)
find_package(CURL REQUIRED)
# Include directories for the libraries
include_directories(${ZLIB_INCLUDE_DIRS})
include_directories(${CURL_INCLUDE_DIR})
# Add source files
add_executable(${PROJECT_NAME})
target_sources(${PROJECT_NAME}
PUBLIC
FILE_SET all_my_modules TYPE CXX_MODULES
FILES
src/testmod.ixx
)
target_sources(${PROJECT_NAME}
PUBLIC
src/main.cpp
)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
# Find local libraries
# link_directories(local_lib/lib)
# Link the libraries
target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${CURL_LIBRARIES})
version: '3'
services:
cpp20devcontainer:
build:
context: .
restart: always
container_name: cpp20devcontainer
env_file:
- ".env"
volumes:
- ./:/var/dev
network_mode: "bridge"
extra_hosts:
- "host.docker.internal:host-gateway"
# Portable C++ Build Environment
#
# The purpose of this file is to provide a consistent development and execution environment for this C++20 project.
# The core idea is that all dependencies are built into this container, and it should easily be able to compile a build right away, regardless of platform.
#
# Once this image is built and it's container is running, you should be able to use a modern tool such as VSCode or NVim to establish a remote
# development instance inside. You could edit the files on the host machine like normal too, but your editor will likely have no idea where
# dependenices are and provide little to no help, such as with VisualStudio's IntelliSense.
FROM ubuntu:23.10
LABEL Description="Portable Build Environment for C++20 w/ Modules using the LLVM/Clang Toolchain"
ENV HOME /var/dev
WORKDIR ${HOME}
SHELL ["/bin/bash", "-c"]
RUN apt-get update && apt-get -y --no-install-recommends install \
git \
curl \
fuse \
libssl-dev \
zlib1g-dev \
libcurl4-openssl-dev \
ca-certificates \
ninja-build \
build-essential \
gdb \
wget \
gdebi-core \
### vvv these are required by the LLVM.sh script
lsb-release \
wget \
software-properties-common \
gnupg \
&& update-ca-certificates
# Install LLVM/Clang Toolchain
ARG LLVM_VERSION=17
RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -
RUN apt-get update && apt-get -y --no-install-recommends install \
libllvm${LLVM_VERSION} \
llvm-${LLVM_VERSION} \
llvm-${LLVM_VERSION}-dev \
llvm-${LLVM_VERSION}-doc \
llvm-${LLVM_VERSION}-examples \
llvm-${LLVM_VERSION}-runtime \
clang-${LLVM_VERSION} \
clang-tools-${LLVM_VERSION} \
clang-${LLVM_VERSION}-doc \
libclang-common-${LLVM_VERSION}-dev \
libclang-${LLVM_VERSION}-dev \
libclang1-${LLVM_VERSION} \
clang-format-${LLVM_VERSION} \
python3-clang-${LLVM_VERSION} \
clangd-${LLVM_VERSION} \
clang-tidy-${LLVM_VERSION} \
lldb-${LLVM_VERSION} \
lld-${LLVM_VERSION} \
libc++-${LLVM_VERSION}-dev \
libc++abi-${LLVM_VERSION}-dev
RUN ln -s /usr/bin/clang-${LLVM_VERSION} /usr/bin/clang && \
ln -s /usr/bin/clang++-${LLVM_VERSION} /usr/bin/clang++ && \
ln -s /usr/bin/lldb-${LLVM_VERSION} /usr/bin/lldb && \
ln -s /usr/bin/lld-${LLVM_VERSION} /usr/bin/lld && \
ln -s /usr/bin/clangd-${LLVM_VERSION} /usr/bin/clangd && \
ln -s /usr/bin/clang-tools-${LLVM_VERSION} /usr/bin/clang-tools && \
ln -s /usr/bin/clang-format-${LLVM_VERSION} /usr/bin/clang-format && \
ln -s /usr/bin/clang-tidy-${LLVM_VERSION} /usr/bin/clang-tidy && \
ln -s /usr/bin/python3-clang-${LLVM_VERSION} /usr/bin/python3-clang
# Install latest CMake
ARG CMAKE_VERSION=3.28.2
ADD https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}-linux-x86_64.sh /cmake-${CMAKE_VERSION}-Linux-x86_64.sh
RUN mkdir /opt/cmake
RUN sh /cmake-${CMAKE_VERSION}-Linux-x86_64.sh --prefix=/opt/cmake --skip-license
RUN ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake
RUN cmake --version
# Install NeoVim
ARG NEOVIM_VERSION=latest
RUN cd ${HOME} && \
wget --no-check-certificate --quiet \
https://github.com/neovim/neovim/releases/${NEOVIM_VERSION}/download/nvim.appimage && \
chmod u+x nvim.appimage && \
./nvim.appimage --appimage-extract && \
./squashfs-root/AppRun --version && \
mv squashfs-root / && \
ln -s /squashfs-root/AppRun /usr/bin/nvim && \
nvim --version
# (Optional) Copy files into environment. You may instead want to use a volume mount.
COPY . .
# keep the container running indefinitely
CMD ["tail", "-f", "/dev/null"]
import testmod;
#include <iostream>
int main(int argc, char *argv[]) {
testfunction();
// we're done here sonny jim
return EXIT_SUCCESS;
}
module;
#include <iostream>
export module testmod;
export void testfunction() {
std::cout << "Greetings from TestModule" << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment