2

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?

Biffen
  • 6,249
  • 6
  • 28
  • 36
haresh
  • 106
  • 1
  • 6
  • 1
    It seems like you should create a *third* repository (say `SourceCommon`), for the common files and code. And its header should be C only. – Some programmer dude Jun 04 '21 at 05:49
  • 2
    set up automated build which compiles headers as C++ and as C, and if build fails, PR cannot be merged – qrdl Jun 04 '21 at 05:51
  • 1
    Add a c file to SourceA which includes all the headers? – Alan Birtles Jun 04 '21 at 06:15
  • 2
    I would say this is awful solution, but You may try. In sourceA, create some file(temp.c), which should be compiled as C code at the same time as rest of the project as cpp. All your common header should be included in it. And if project with 2 types of file will compile, your sourceB will compile too – Deumaudit Jun 04 '21 at 06:30
  • C and C++ are *different* languages, so your header files should be restricted to common elements of both? I'd anticipate a problem with that. I think that I'd make to design decision now to have different headers for each language. (_eg_ https://stackoverflow.com/questions/776283/what-does-the-restrict-keyword-mean-in-c) – Neil Jun 04 '21 at 06:38
  • 2
    Don't answer in the comments people – Hong Ooi Jun 04 '21 at 07:00

1 Answers1

2

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396