Skip to content

Commit 360754b

Browse files
Add method for specifying C/C++ standard version (#761)
1 parent bf4f709 commit 360754b

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub struct Build {
108108
cpp_set_stdlib: Option<Arc<str>>,
109109
cuda: bool,
110110
cudart: Option<Arc<str>>,
111+
std: Option<Arc<str>>,
111112
target: Option<Arc<str>>,
112113
host: Option<Arc<str>>,
113114
out_dir: Option<Arc<Path>>,
@@ -314,6 +315,7 @@ impl Build {
314315
cpp_set_stdlib: None,
315316
cuda: false,
316317
cudart: None,
318+
std: None,
317319
target: None,
318320
host: None,
319321
out_dir: None,
@@ -708,6 +710,37 @@ impl Build {
708710
self
709711
}
710712

713+
/// Specify the C or C++ language standard version.
714+
///
715+
/// These values are common to modern versions of GCC, Clang and MSVC:
716+
/// - `c11` for ISO/IEC 9899:2011
717+
/// - `c17` for ISO/IEC 9899:2018
718+
/// - `c++14` for ISO/IEC 14882:2014
719+
/// - `c++17` for ISO/IEC 14882:2017
720+
/// - `c++20` for ISO/IEC 14882:2020
721+
///
722+
/// Other values have less broad support, e.g. MSVC does not support `c++11`
723+
/// (`c++14` is the minimum), `c89` (omit the flag instead) or `c99`.
724+
///
725+
/// For compiling C++ code, you should also set `.cpp(true)`.
726+
///
727+
/// The default is that no standard flag is passed to the compiler, so the
728+
/// language version will be the compiler's default.
729+
///
730+
/// # Example
731+
///
732+
/// ```no_run
733+
/// cc::Build::new()
734+
/// .file("src/modern.cpp")
735+
/// .cpp(true)
736+
/// .std("c++17")
737+
/// .compile("modern");
738+
/// ```
739+
pub fn std(&mut self, std: &str) -> &mut Build {
740+
self.std = Some(std.into());
741+
self
742+
}
743+
711744
/// Set warnings into errors flag.
712745
///
713746
/// Disabled by default.
@@ -1613,6 +1646,14 @@ impl Build {
16131646
println!("Info: default compiler flags are disabled");
16141647
}
16151648

1649+
if let Some(ref std) = self.std {
1650+
let separator = match cmd.family {
1651+
ToolFamily::Msvc { .. } => ':',
1652+
ToolFamily::Gnu | ToolFamily::Clang => '=',
1653+
};
1654+
cmd.push_cc_arg(format!("-std{}{}", separator, std).into());
1655+
}
1656+
16161657
if let Ok(flags) = self.envflags(if self.cpp { "CXXFLAGS" } else { "CFLAGS" }) {
16171658
for arg in flags {
16181659
cmd.push_cc_arg(arg.into());

tests/test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,14 @@ fn gnu_no_dash_dash() {
366366
test.cmd(0).must_not_have("--");
367367
}
368368

369+
#[test]
370+
fn gnu_std_c() {
371+
let test = Test::gnu();
372+
test.gcc().file("foo.c").std("c11").compile("foo");
373+
374+
test.cmd(0).must_have("-std=c11");
375+
}
376+
369377
#[test]
370378
fn msvc_smoke() {
371379
reset_env();
@@ -443,6 +451,14 @@ fn msvc_no_dash_dash() {
443451
test.cmd(0).must_not_have("--");
444452
}
445453

454+
#[test]
455+
fn msvc_std_c() {
456+
let test = Test::msvc();
457+
test.gcc().file("foo.c").std("c11").compile("foo");
458+
459+
test.cmd(0).must_have("-std:c11");
460+
}
461+
446462
// Disable this test with the parallel feature because the execution
447463
// order is not deterministic.
448464
#[cfg(not(feature = "parallel"))]

0 commit comments

Comments
 (0)