How to prevent users writing C++ code in common header files between C and C++ project?

Viewed 125

We have two projects. One project is C++ (say SourceA) and other is only C (say SourceB). Both have different repository but few header files are common between them. While compiling SourceB I copy common header files from SourceA.

Since AsourceA is C++ code, I wanted user to prevent writing any C++ code in common header files, so that it will also compile in SourceB. Is there any way to do so? I tried using extern "c"{} but C++ code inside extern "C" is not giving error.

I am using Eclipse IDE for SourceA, if there is any setting Eclipse then it can also work.

In general how people handle this problem?

1 Answers

Generally if it is libraries shared among multiple project, you can place them in a "company standard" repo of their own. Then the project-specific versions of each file will be local to that project, and in case anyone make changes to the file and commits to the project-specific repo, it won't affect any other project.

Then when the "company standard" files need to be updated, you update them in their own repo, then let each project update as needed, by checking the diff to see if there were any project-specific changes that need to be preserved.

That is, check out the latest version of all files from the "company standard" repo in one location separate from your local project. Then update your local project files by using a diff tool. Check which files that are different and if any were found, check what was changed. And then finally commit to the project-specific repo.

You could also write protect the actual files by setting that file attribute. Then nobody should edit them by mistake, as the IDE will give some message about it.

Related