Automatically set --pgmcxx for C++ sources when a C++ compiler is available
type: bugneeds triage
**Describe the bug**
Cabal does not set `-pgmcxx` for GHC when compiling C++ sources, even when a separate C++ compiler (`g++`) is available. By default, GHC falls back to the same compiler used for C, which does **not** automatically link the C++ standard library. This forces users to add `extra-libraries: stdc++` to every project using `cxx-sources`.
This is particularly problematic for GHC 9.4+ and for users who install GHC via ghcup, as ghcup always provides both `gcc` and `g++`. Moreover, Cabal's own `configureToolchain` and `runConfigureScript` logic already uses `g++` in some places, leading to an inconsistency where C++ sources compiled through GHC are treated differently (with `gcc`) than other C++ compilation steps.
**To Reproduce**
1. Create a minimal project with a C++ source file, intentionally **without** `-pgmcxx` or `extra-libraries`:
```cabal
executable test-cxx
main-is: Main.hs
build-depends: base
default-language: Haskell2010
include-dirs: cxxbits
cxx-sources: cxxbits/lib.cpp
```
2. Try to build:
```
$ cabal build
```
Observe the linking error due to missing `stdc++`.
**Expected behavior**
If `g++` is present, Cabal should automatically pass `-pgmcxx=g++` to GHC, so that C++ sources are compiled and linked correctly without manual `extra-libraries`. The project above should build successfully out-of-the-box, and the behaviour should be consistent with how `configureToolchain` already prefers `g++` for C++-related tasks.
**Additional context**
- Addressed by PR #11763
- The internal `configureToolchain` and `runConfigureScript` already use `g++`, creating inconsistency with C++ source compilation through GHC.
0 条评论