emscripten-core / emscripten-core/emscripten

emcc has different behaviour than clang for '-x' command line option.

Open
#14,604 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

When compiling C and C++ files in the same emcc invocation and using the '-x' command line option to force C vs C++ compilation regardless of file extension, emcc has different behaviour from clang in at least the following situation:

Unpack the two source files bla.c and blub.cc from the following archive:

[sources.zip](https://github.com/emscripten-core/emscripten/files/6785345/sources.zip)

The C file contains a "C-ism" and doesn't compile in C++ mode while the .cc file only compiles in C++ mode.

Now run clang like this to compile both files into an executable (tested with Apple's clang, also, please ignore for now that the '-x c++' isn't really necessary in this case):

```sh
> clang bla.c -x c++ blub.cc -o bla
```

This works, now try the same with emcc:

```sh
> emcc bla.c -x c++ blub.cc -o bla
```
This fails because the .c file is compiled as C++.

And the following cases also have different behaviour:

```sh
# this works:
> clang -x c bla.c -x c++ blub.cc -o bla
# this fails because bla.c is compiled as C++:
> emcc -x c bla.c -x c++ blub.cc -o bla
```

...and the other way around:
```sh
# this works:
> clang -x c++ blub.cc -x c bla.c -o bla
# this fails because blub.cc is compiled as C:
> emcc -x c++ blub.cc -x c bla.c -o bla
```

The exact versions used are:
```sh
> clang --version
Apple clang version 12.0.5 (clang-1205.0.22.11)...
> emcc --version
...2.0.25...
```

(note that it's the **second** ```-x``` option which seems to define the language mode for all(?) source files in the command line - just a guess, but maybe a new '-x' option overrides any previous options... ~~maybe the options are gathered in a Python dictionary before they're passed to clang?~~

Also see this discussion thread and post for context:

https://groups.google.com/g/emscripten-discuss/c/KkaLQN0zKN4/m/wm_XJcGzAAAJ

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.