Search code examples
c++clinkername-mangling

Why prevent Name-mangling C files


Note: my question is for my knowledge im not trying to solve particular problem.

i was looking into using C library in CPP project to prevent compiler name mangling:

#ifdef __cplusplus
extern "C" {
#endif

1- but i also, read that C++ compiler will not mangle .c files. so why bother?

2- if you had a library of +500 c files. (headers + c files). how do you auto add this Extern "C" into them? is there a tool that automate this process?


Solution

    1. c++ compiler may not do name mangling on .c files, but it definitely would mangle those included by .cpp files, so in header files it is needed
    2. you can simply use extern "C"{ file content here }, this can be easily done in many tools.

    I write a cmd script (.bat) for you

    WARNING : this would overwrite existing files, make a backup!

    @ECHO OFF
    for /R %%f in (*.c,*.h) do (
       @echo extern "C" { > temp
       @echo. >> temp
       @type %%f >> temp
       @echo. >> temp   
       @echo } >> temp
       @type temp > %%f
       @echo processed %%f
    )