Skip to content

Commit

Permalink
Fix the compilation of OCaml 5 programs on Cygwin
Browse files Browse the repository at this point in the history
strdup is not part of the C standard library but is part of POSIX.
By default GCC and Clang both use the GNU variant of the C standard,
so we are using strdup here to ensure users have access to it by default.
If -std=c++11 is used instead of -std=gnu++11 this would fail to compile
on non-POSIX platforms such as Cygwin.

Signed-off-by: Kate <kit-ty-kate@outlook.com>
  • Loading branch information
kit-ty-kate committed Oct 10, 2024
1 parent 9992794 commit 16c2169
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/dune_rules/cxx_flags.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let base_cxx_compile_flags version = function
| Gcc | Clang ->
"-x"
:: "c++"
:: (if Ocaml.Version.add_std_cxx_flag version then [ "-std=c++11" ] else [])
:: (if Ocaml.Version.add_std_cxx_flag version then [ "-std=gnu++11" ] else [])
| Msvc -> [ "/TP" ]
| Other _ -> []
;;
Expand Down
8 changes: 7 additions & 1 deletion test/blackbox-tests/test-cases/cpp-std-ocaml5.t/cpp11.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#include <caml/misc.h>
#include <caml/mlvalues.h>
#include <iostream>
#include <cstring>

// strdup is not part of the C standard library but is part of POSIX.
// By default GCC and Clang both use the GNU variant of the C standard,
// so we are using strdup here to ensure users have access to it by default.
// If -std=c++11 is used instead of -std=gnu++11 this would fail to compile
// on non-POSIX platforms such as Cygwin.
extern "C" CAMLprim value cpp11(value _unit) {
std::cout << "Hi from C++11" << std::endl;
std::cout << strdup("Hi from C++11") << std::endl;
return Val_unit;
}

0 comments on commit 16c2169

Please sign in to comment.