# C++ file in kernel



## baller (Apr 7, 2018)

Hi,

I am trying to build the kernel with the addition of a simple C++ file in sys/kern that doesn't use any C++-specific library but only language features such as the use of classes, e.g.

```
// kern/kern_cpp_test.cc
class TestClass {
   int x;
};
```
I have added kern/kern_cpp_test.cc to sys/conf/files (with "standard" configuration), but the file doesn't seem to be recognized as a C++ file for compilation. I checked share/mk/sys.mk and I found:

```
.cc .cpp .cxx .C:
    ${CXX} ${CXXFLAGS} ${LDFLAGS} ${.IMPSRC} ${LDLIBS} -o ${.TARGET}
```

Doing "make buildkernel" results in this error, which looks like the result of the extension being incorrectly recognized as something else and being truncated. The compiler is still cc and not cc++ for some reason:


```
cc -target x86_64-unknown-freebsd11.1 --sysroot=/usr/obj/usr/src/tmp -B/usr/obj/usr/src/tmp/usr/bin -c -O2 -pipe -fno-strict-aliasing  -g -nostdinc  -I. -I/usr/src/sys -I/usr/src/sys/contrib/libfdt -D_KERNEL -DHAVE_KERNEL_OPTION_HEADERS -include opt_global.h  -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -MD  -MF.depend.kern_cpp_test.co -MTkern_cpp_test.co -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float  -fno-asynchronous-unwind-tables -ffreestanding -fwrapv -fstack-protector -gdwarf-2 -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes  -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual  -Wundef -Wno-pointer-sign -D__printf__=__freebsd_kprintf__  -Wmissing-include-dirs -fdiagnostics-show-option  -Wno-unknown-pragmas  -Wno-error-tautological-compare -Wno-error-empty-body  -Wno-error-parentheses-equality -Wno-error-unused-function  -Wno-error-pointer-sign -Wno-error-shift-negative-value -Wno-error-address-of-packed-member  -mno-aes -mno-avx  -std=iso9899:1999 -Werror
cc: error: no input files
```

(Look at "*-MTkern_cpp_test.co*")

Is there any solution to this?


----------



## unitrunker (Apr 7, 2018)

I do not see the source file name anywhere in hat line.


----------



## baller (Apr 7, 2018)

unitrunker said:


> I do not see the source file name anywhere in hat line.



Sorry, fixed the original message (fixed name of the source file). There is mention of the file but with .co extension instead of cc. Is C++ in kernel even supported?


----------



## Deleted member 54719 (Apr 13, 2018)

RE: c++ in kernel mode.

probably not.  there are a lot of debatable reasons why c++ is discouraged for kernel programming in unix/posix type os.  I've written vxworks kernel modules in c++ and you generally have to be very careful about what language features you use, and how to deal with symbol linkages, given that c++ will managle names by default.


----------

