Skip to content

Conversation

@renecannao
Copy link

Summary

This PR modernizes the cpp-dotenv project to build with contemporary C++ toolchains (C++17, Clang 17+, modern CMake).

Changes

  • C++17 upgrade: Changed from C++11 to C++17 standard
  • CMake 3.16: Updated minimum CMake version from 3.10/3.0 to 3.16
  • Logger fix: Added const qualifier to logger::position::less::operator() for modern libc++ compatibility
  • Naming conflict fix: Resolved the namespace/class name ambiguity (both named dotenv) using C++17 inline variables

Technical Details

The dotenv class name matches its namespace, which causes C++ parser ambiguity when defining out-of-line members. This is fixed by:

  • Using C++17 inline variables for static member definitions
  • Adding a typedef alias in the .cpp file to disambiguate the class type

Test Plan

  • Project compiles successfully with Clang 17
  • Static library libcpp_dotenv.a builds without errors
  • All existing functionality preserved

Modernize the project to build with contemporary toolchains:

- Upgrade C++ standard from C++11 to C++17
- Update CMake minimum version to 3.16
- Fix logger comparator const-correctness for modern libc++
- Resolve namespace/class name ambiguity using inline variables
- Use typedef alias to disambiguate out-of-line member definitions

The dotenv class name matches its namespace, which causes parsing
issues in modern C++. Fixed by using C++17 inline variables for
static members and a typedef alias in the implementation.
Add a new CMake option BUILD_BOTH_LIBRARIES that allows building both
static and shared libraries in a single build directory.

This provides an alternative to the existing approach of building in
separate directories with BUILD_SHARED_LIBS=ON/OFF, while maintaining
backward compatibility with existing build scripts.

Usage:
  cmake .. -DBUILD_BOTH_LIBRARIES=ON

Produces:
  - libcpp_dotenv_static.a
  - libcpp_dotenv_shared.so (or .dylib on macOS)
- Add validation to prevent BUILD_BOTH_LIBRARIES and BUILD_SHARED_LIBS
  from being used together (with warning message)
- Improve documentation for the cpp_dotenv alias, explaining that it
  points to cpp_dotenv_static when BUILD_BOTH_LIBRARIES is ON
- Add OUTPUT_NAME property so libraries are named correctly:
  * libcpp_dotenv.a (static) instead of libcpp_dotenv_static.a
  * libcpp_dotenv.so/.dylib (shared) instead of libcpp_dotenv_shared.so
- Enable CMAKE_POSITION_INDEPENDENT_CODE when building shared libraries
  to ensure static dependencies are compiled with -fPIC
- Document BUILD_BOTH_LIBRARIES option in README.md with usage examples

The OUTPUT_NAME property sets only the base name; CMake automatically
adds the correct platform-specific extension (.so on Linux, .dylib on macOS).
- Fix README CMake version mismatch: update from >=3.10 to >=3.16
  to match CMakeLists.txt requirement
- Fix Windows MSVC import library conflict: when BUILD_BOTH_LIBRARIES is ON,
  both static and shared libraries would produce cpp_dotenv.lib on Windows.
  Add IMPORT_SUFFIX "_shared.lib" to the shared library's import library
  to avoid this conflict on Windows/MSVC.
Add BUILD_BOTH_LIBRARIES option for TAP tests
dotenv.h:
- Clarify inline variable syntax with explanatory comment
- Add comment explaining why _instance cannot be inline inside class

dotenv.cpp:
- Rename Dotenv_Type to DotenvClass for better C++ conventions
- Fix operator[] to use typedef alias consistently (avoid parsing issues)
- Improve comment clarity for the global env variable definition

CMakeLists.txt:
- Add MSVC support for compiler flags (/W4 /Od /O2)
- Improve Windows library naming: use distinct OUTPUT_NAME on MSVC
  to avoid conflicts between static and shared library .lib files

common/libs/antlr4-cpp-runtime/CMakeLists.txt:
- Update cmake_minimum_required from 3.10 to 3.16 for consistency
Address remaining Copilot feedback: env_filename is kept for backward
compatibility as external code may reference dotenv::dotenv::env_filename
directly. Add explanatory comment to document this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant