From 85ee736a938bc882ee8cb2eb312c9b4c66f53a6c Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Tue, 24 Nov 2020 10:34:55 +0100 Subject: [PATCH 1/2] Test C and C++ compilation explicitly --- tests/BUILD.bazel | 5 +++++ tests/c-test.c | 7 +++++++ tests/cc-test.cc | 6 +++++- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/c-test.c diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 077c0c845..6c80584fd 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -82,6 +82,11 @@ cc_binary( srcs = ["cc-test.cc"], ) +cc_binary( + name = "c-test", + srcs = ["c-test.c"], +) + # Test that nixpkgs_cc_configure is selected. cc_toolchain_test( name = "cc-toolchain", diff --git a/tests/c-test.c b/tests/c-test.c new file mode 100644 index 000000000..304d0d872 --- /dev/null +++ b/tests/c-test.c @@ -0,0 +1,7 @@ +#include "stdio.h" +int main() { + puts("Hello world\n"); + // Ensure that this is compiled as C, template is a keyword in C++. + int template = 0; + return template; +} diff --git a/tests/cc-test.cc b/tests/cc-test.cc index 76e819701..0e7f60bbd 100644 --- a/tests/cc-test.cc +++ b/tests/cc-test.cc @@ -1 +1,5 @@ -int main() { return 0; } +#include +int main() { + std::cout << "Hello world\n"; + return 0; +} From 6d6fc1b608a79ac2036688d640c3d13c68e158f2 Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Tue, 24 Nov 2020 12:07:35 +0100 Subject: [PATCH 2/2] Clang compatibility Add `-x c++` to `CXX_FLAGS` to enable C++ compilcation with `clang`. Shift `-lstdc++` and `-lm` into `LINK_LIBS` to avoid missing symbol errors when linking C++ with `clang`. --- nixpkgs/toolchains/cc.nix | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nixpkgs/toolchains/cc.nix b/nixpkgs/toolchains/cc.nix index a234e3276..e2c766a0d 100644 --- a/nixpkgs/toolchains/cc.nix +++ b/nixpkgs/toolchains/cc.nix @@ -160,7 +160,10 @@ in # Keep stack frames for debugging, even in opt mode. -fno-omit-frame-pointer ) - CXX_FLAGS=(-std=c++0x) + CXX_FLAGS=( + -x c++ + -std=c++0x + ) LINK_FLAGS=( $( if [[ -x ${cc}/bin/ld.gold ]]; then echo -fuse-ld=gold; fi @@ -176,10 +179,11 @@ in # Have gcc return the exit code from ld. add_compiler_option_if_supported -pass-exit-codes ) + ) + LINK_LIBS=( -lstdc++ -lm ) - LINK_LIBS=() OPT_COMPILE_FLAGS=( # No debug symbols. # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or