opened
sumeetchhetri
username_0
<!--

Please title your PR as follows: `time: fix foo bar`.
Always start with the thing you are fixing, then describe the fix.
Don't use past tense (e.g. "fixed foo bar").

Explain what your PR does and why.

If you are adding a new function, please document it and add tests:

```
// foo does foo and bar
fn foo() {

// file_test.v
fn test_foo() {
    assert foo() == ...
    ...
}
```

If you are fixing a bug, please add a test that covers it.

Before submitting a PR, please:
  A) run the tests with `v test-compiler` .
  B) make sure, that V can still compile itself:
```shell
./v -o v cmd/v
./v -o v cmd/v
```

I try to process PRs as soon as possible. They should be handled within 24 hours.

Applying labels to PRs is not needed.

Thanks a lot for your contribution!

-->
Add Preliminary support for C++ compiler
issue
created
sumeetchhetri
username_0
Sample program tested with both c as well as c++ compiler on Ubuntu

v run test.v --- c version
VFLAGS="-cc c++" v -cflags '-std=gnu11 -Wall -O3 -march=native -mtune=native -fpermissive -W -Wall -Wpointer-arith -Wno-unused-parameter -w -fpermissive -Wwrite-strings' -cg -keepc run test.v --- c++ version

[test.txt](https://github.com/vlang/v/files/4567938/test.txt)
comment
created
sumeetchhetri
username_0
This PR has a few memory leaks in the form of the new operator (for allocation of c style arrays -- aka inline initialized arrays). I have used the _cplusplus defines in order to allocate the new arrays, these need to be de-allocated somewhere in the new_array_from_c_array function (but this is not straightforward) and similarly for new_map_init function.
comment
created
spytheman
username_1
What kind of problem does that solve?
V transpiles to c99 code. Why should it also conform to the expectations of C++ compilers???
comment
created
spytheman
username_1
Would we also then need to add a C++ CI job too? On which platforms, and which compilers would it run?
comment
created
spytheman
username_1
Are there platforms that have C++ compilers, but lack C99 ones?
comment
created
medvednikov
username_2
Indeed, C++ is a very different language (malloc return type for example makes a huge difference) and goes against V's philosophy of simplicity and minimalism. All platforms have a C compiler, and there are no platforms that have a C++ compiler, but no C compiler.

I'm fine with solving things like `new` variables, but I don't see the point in complicating things to get C++ support.
comment
created
medvednikov
username_2
We already have a lot of backends to maintain (C, x64, JS, potentially LLVM), no need in another one, when C works just fine.
comment
created
sumeetchhetri
username_0
Thanks Alex/Delyan for the quick response. I'll choose to maintain the C++ supporting version for my own needs then. Thanks guys.
comment
created
medvednikov
username_2
It's possible to mix C and C++ and use separate compilers.

https://embeddedartistry.com/blog/2017/05/01/mixing-c-and-c-extern-c/

Have you considered that?
comment
created
RiordanIX
username_3
Could some of this could be used in the C++ -> V transpilation? Or does that need to be its own module?
comment
created
sumeetchhetri
username_0
Here is my initial version of the [vweb](https://github.com/username_0/ffead-cpp/blob/v4.0/langs/v/vweb.v) server serving the ffead-cpp framework, will move it to the pico.v backend next. Thanks Alex, I initially started with the c interface option but thought of extending the v code itself to support my use case which now I think was too much effort. Finally had to resort to my final option of doing this with the c interface. Just a POC for now, but a good start anyways, also picked up v programming on the way.
Thanks everyone.
comment
created
medvednikov
username_2
@username_0 I think in the future we can have C++ support, won't hurt. So I'm open to merging the changes into master.
comment
created
spytheman
username_1
@username_2 I am against merging this, unless there is a new CI job that tracks whether V continues to work with a c++ compiler. Otherwise maintaining that the generate code remains C++ compatible will be very hard.
comment
created
sumeetchhetri
username_0
Have created a new PR - https://github.com/vlang/v/pull/4756. Let's keep this PR open until the new one gets merged.
comment
created
sumeetchhetri
username_0
Closing the PR
comment
created
kaijaa
username_4
[]()
comment