0

I have a long-standing C library used by C and C++ programs.

It has a few compilation units (in other words, C++ source files) that are totally C++, but this has never been a problem. My understanding is that linkers (at least, Linux, Windows, etc.) always work at the file-by-file level so that an object file in a library that isn't referred to, doesn't have any effect on the linking, isn't put in the binary, and so on. The C users of the library never refer to the C++ symbols, and the library doesn't internally, so the resulting linked app is C-only. So while it's always worked perfectly, I don't know if it's because the C++ doesn't make it past the linking stage, or because more deeply, this kind of mixing would always work even if I did mix languages.

For the first time I'm thinking of adding some C++ code to the existing C API's implementation.

For purposes of discussion let us say I have a C function that does something, and logs it via stdout, and since this is buffered separately from cout, the output can become confusing. So let us say this module has an option that can be set to log to cout instead of stdout. (This is a more general question, not merely about getting cout and stdout to cooperate.) The C++ code might or might not run, but the dependencies will definitely be there.

In what way would this impact users of this library? It is widely used so I cannot check with the entire user base, and as it's used for mission-critical apps it'd be unacceptable to make a change that makes links start failing, at least unless I supply a release note explaining the problem and solution.

Just as an example of a possible problem, I know compilers have "hidden" libraries of support functions that are necessary for C and C++ programs. There are obviously also the Standard C and C++ libraries, that normally you don't have to explicitly link to. My concerns are that the compiler might not know to do these things.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Swiss Frank
  • 1,985
  • 15
  • 33
  • "let us say". That is not a good way to explain what you are trying to do. There are too many generalities and unclear points in your description. Please construct a [minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) that illustrates the concepts and problems that you are trying to understand or solve. – kaylum Apr 19 '20 at 05:44
  • 1
    By default `cin` and `cout` synchronize with good ol' C IO. They had to because if you're calling C libraries, and you do fairly frequently, things will get messy. In fact there is a cargo cult that reflexively turns off this synchronization. See [Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);](https://stackoverflow.com/questions/31162367/significance-of-ios-basesync-with-stdiofalse-cin-tienull) for details if you are interested. – user4581301 Apr 19 '20 at 05:48
  • Is the library linked statically (to `.a` or `.lib`) or dynamically (to `.so` or `.dll` or `.dylib`)? – numzero Apr 19 '20 at 15:58
  • I'm using it on Linux as a .a and .so, and on Windows as a .lib and .dll . – Swiss Frank Apr 19 '20 at 16:13
  • _By default cin and cout synchronize with good ol' C IO._ -- to be clear this has nothing to do with cin or cout. As to a test case, I really couldn't have been clearer that my worry isn't to show I have a problem in a specific case (which I could give you a test example for) but rather to know a priori that there are no cases where it would fail. Unless you want me to generate all possible programs, I don't see how I can give you that as a test suite. – Swiss Frank Apr 26 '20 at 11:41

0 Answers0