From aba50e730000e74bc8723821a03193c118e703ea Mon Sep 17 00:00:00 2001 From: cspark Date: Thu, 20 Nov 2025 01:29:00 +0000 Subject: [PATCH 1/4] Switch to cmake and cc-ls --- .gitignore | 3 ++- CMakeLists.txt | 18 ++++++++++++++++++ compile_commands.json | 21 +-------------------- default.nix | 8 ++++++++ main.cpp | 15 +++++++++++++-- run.sh | 6 +++--- 6 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 CMakeLists.txt mode change 100644 => 120000 compile_commands.json create mode 100644 default.nix diff --git a/.gitignore b/.gitignore index b2f55fd..ea4b235 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +build/ .cache/ -a.out +.ccls-cache/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..0699e16 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.23) + +project(discern) + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +#string(REPLACE ":" ";" INCLUDE_LIST $ENV{CMAKE_LIBRARY_PATH}) + +#include_directories(${INCLUDE_LIST}) + + +add_executable(discern) + +target_sources(discern + PRIVATE + main.cpp +) diff --git a/compile_commands.json b/compile_commands.json deleted file mode 100644 index a808858..0000000 --- a/compile_commands.json +++ /dev/null @@ -1,20 +0,0 @@ -[ - { - "arguments": [ - "/run/current-system/sw/bin/clang++", - "-std=c++23", - "-resource-dir", - "/nix/store/k2s6xfca52n25yb8b6f58i87kszp3m7g-clang-wrapper-21.1.2/resource-root", - "-idirafter", - "/nix/store/dqgfdpr53ldqmdfmginb1z6kzslkzsdl-glibc-2.40-66-dev/include", - "-isystem", - "/nix/store/68ndh04pl2hhhizsarvzwa9cnlp7zj3d-gcc-14.3.0/include/c++/14.3.0", - "-isystem", - "/nix/store/68ndh04pl2hhhizsarvzwa9cnlp7zj3d-gcc-14.3.0/include/c++/14.3.0/x86_64-unknown-linux-gnu", - "-c", - "main.cpp" - ], - "directory": "/home/cspark/Projects/CPP/hello_world", - "file": "/home/cspark/Projects/CPP/hello_world/main.cpp" - } -] diff --git a/compile_commands.json b/compile_commands.json new file mode 120000 index 0000000..25eb4b2 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +build/compile_commands.json \ No newline at end of file diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..83b1569 --- /dev/null +++ b/default.nix @@ -0,0 +1,8 @@ +with (import {}); + +pkgs.mkShell { + name = "C++ Study Repository"; + buildInputs = with pkgs; [ + cmake + ]; +} diff --git a/main.cpp b/main.cpp index 01ed3a0..3b27157 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,17 @@ -#include #include +#include +#include + +typedef struct foo { + uint8_t test; + + int one() { + return 1; + }; +} foo; int main() { - std::println("Hello, world!"); + foo ok; + std::cout << std::to_string(ok.one()) << std::endl; } + diff --git a/run.sh b/run.sh index 120684f..2b9c3be 100755 --- a/run.sh +++ b/run.sh @@ -1,7 +1,7 @@ #!/bin/sh -rm a.out +cmake -B build --fresh -clang++ -std=c++23 main.cpp +cmake --build build -./a.out +./build/discern From 98c071d7eeea7369866f742d84ce2fb49d04858a Mon Sep 17 00:00:00 2001 From: cspark Date: Thu, 20 Nov 2025 01:30:17 +0000 Subject: [PATCH 2/4] Update README --- README.org | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/README.org b/README.org index 84a769f..2c1d464 100644 --- a/README.org +++ b/README.org @@ -1,16 +1,4 @@ * NixOS C++ Dev Template -Having issues getting clangd to detect std header files despite clang++ and g++ having no issues. So this template/workaround solves this. +Due to issues with clangd, switched to cc-ls. -I assume this is due to some wrapper script nonsense. - -As a workaround I use bear to generate the necessary paths/arguments to get clangd to pick these up. - -#+BEGIN_SRC -bear -- clang++ main.cpp -#+END_SRC - -In the generated compile_commands.json. There is a command for generating the main project but also a temporary object file. - -I assume this is more weird wrapper stuff (Bear does not support nixos clang wrappers) - -You need to move the cxx_isystem related flags as these are where the c++ stdlibs are kept. Also just rename them to isystem otherwise they dont work for whatever reason. +Also now a basic CMake template. From 53b9bb2d72a8a275224b35c5388e068c817bcf75 Mon Sep 17 00:00:00 2001 From: cspark Date: Thu, 20 Nov 2025 02:21:17 +0000 Subject: [PATCH 3/4] FINALLY get clangd working --- CMakeLists.txt | 6 ++---- README.org | 6 ++---- default.nix | 11 ++++++++--- main.cpp | 13 +------------ 4 files changed, 13 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0699e16..6a28b4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,10 +5,8 @@ project(discern) set(CMAKE_CXX_STANDARD 23) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -#string(REPLACE ":" ";" INCLUDE_LIST $ENV{CMAKE_LIBRARY_PATH}) - -#include_directories(${INCLUDE_LIST}) - +#string(REPLACE ":" ";" INCLUDE_LIST $ENV{CMAKE_STDLIB}) +#include_directories($ENV{CMAKE_STDLIB}) add_executable(discern) diff --git a/README.org b/README.org index 2c1d464..bf84313 100644 --- a/README.org +++ b/README.org @@ -1,4 +1,2 @@ -* NixOS C++ Dev Template -Due to issues with clangd, switched to cc-ls. - -Also now a basic CMake template. +* Nix C++ Dev Template +Basic C++ CMake template with Nix diff --git a/default.nix b/default.nix index 83b1569..ac131bd 100644 --- a/default.nix +++ b/default.nix @@ -1,8 +1,13 @@ with (import {}); -pkgs.mkShell { - name = "C++ Study Repository"; - buildInputs = with pkgs; [ +clangStdenv.mkDerivation { + name = "discern"; + nativeBuildInputs = with pkgs; [ cmake + + clang-tools + clang ]; + + CXXFLAGS = "-isystem${pkgs.gcc.cc}/include/c++/14.3.0 -isystem${pkgs.gcc.cc}/include/c++/14.3.0/x86_64-unknown-linux-gnu -isystem${pkgs.glibc.dev}/include"; } diff --git a/main.cpp b/main.cpp index 3b27157..8ec9f09 100644 --- a/main.cpp +++ b/main.cpp @@ -1,17 +1,6 @@ -#include -#include #include -typedef struct foo { - uint8_t test; - - int one() { - return 1; - }; -} foo; - int main() { - foo ok; - std::cout << std::to_string(ok.one()) << std::endl; + std::cout << "Hello, world!" << std::endl; } From e365dc635477dd2606d38b5a307e5d508db49f9d Mon Sep 17 00:00:00 2001 From: cspark Date: Thu, 20 Nov 2025 02:25:22 +0000 Subject: [PATCH 4/4] Finalise template --- CMakeLists.txt | 6 +++--- default.nix | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a28b4b..c922b4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.23) -project(discern) +project(template) set(CMAKE_CXX_STANDARD 23) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -8,9 +8,9 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) #string(REPLACE ":" ";" INCLUDE_LIST $ENV{CMAKE_STDLIB}) #include_directories($ENV{CMAKE_STDLIB}) -add_executable(discern) +add_executable(template) -target_sources(discern +target_sources(template PRIVATE main.cpp ) diff --git a/default.nix b/default.nix index ac131bd..e722ad8 100644 --- a/default.nix +++ b/default.nix @@ -1,13 +1,14 @@ with (import {}); clangStdenv.mkDerivation { - name = "discern"; - nativeBuildInputs = with pkgs; [ - cmake + name = "template"; + nativeBuildInputs = with pkgs; [ + cmake - clang-tools - clang - ]; + clang-tools + clang + ]; - CXXFLAGS = "-isystem${pkgs.gcc.cc}/include/c++/14.3.0 -isystem${pkgs.gcc.cc}/include/c++/14.3.0/x86_64-unknown-linux-gnu -isystem${pkgs.glibc.dev}/include"; + # Workaround as Clangd cannot see std header files, so include set of files manually just for Clangd + CXXFLAGS = "-isystem${pkgs.gcc.cc}/include/c++/14.3.0 -isystem${pkgs.gcc.cc}/include/c++/14.3.0/x86_64-unknown-linux-gnu -isystem${pkgs.glibc.dev}/include"; }