2

Header files are a necessity in C/C++/ObjC because each file needs the definition of all its symbols before being compiled into an object file. One side-effect is that a library distributor that do not want to open its source code can provide a client only with a header file and the .o to be linked. Is this possible in languages that rely on a full view of the source code during compilation, like Java? Are there any other languages with interesting solutions for this use case?

To provide a bit of context: In my (5y+) experience as a software engineer, I've only ever used libraries to which I have direct source code access in Java, Python and Go, and have never developed closed-source libraries. For the first two a closed-source developer could ship bytecode, but I don't see how it's possible to do that with Go, which doesn't rely on header files to forward-declare symbols.

Bruno Kim
  • 2,300
  • 4
  • 17
  • 27
  • Preemptive strike: 1) this question does not concerns *whether* it's interesting to use closed source or a remote service or open source; 2) I don't have a problem at hands right now, always been relying on open source; 3) I'm not satisfied with the answers that everyone's favorite search engine provides, would love that some *experts exchange* knowledge. – Bruno Kim Feb 14 '18 at 00:59
  • You're 'not satisfied with the answers that everyone's favourite search engines provide' such as what? – user207421 Feb 14 '18 at 01:15
  • Results for [closed source python] say that distributing .pyc can be done though it's not advisable. [header files rationale] has some discussion on why they were necessary, but don't go deeper on modern options (https://stackoverflow.com/questions/1507743/whats-the-rationale-behind-headers). Simple searches for [closed source] yield only discussions on why you shouldn't want that. I'm not satisfied with these results because closed source seems... anathema. I can envision real use cases for sharing an object file but not the source (e.g., trade secrets). – Bruno Kim Feb 15 '18 at 01:10

1 Answers1

3

Is this possible in languages that rely on a full view of the source code during compilation, like Java?

Bzzt. Java does not 'rely on a full view of the source code during compilation'. You don't need the source code to use a Java JAR file, right? The export symbol information is in the .class file. Consider the JDK's rt.jar as the most trivial example. If that didn't work, nothing would. And you don't need the source code for that.

In Modula, Ada, etc, it is also in the object code, somehow.

It is really just about only C and C++ that don't have this feature, and therefore must rely on header files.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • You're right, .class is bytecode, not source code. Still, as far as I know Java can't compile a .class in isolation, without reading symbols from its dependencies. That's the "full view" I was thinking, in opposition of a "narrow view" that is achievable in C/C++/ObjC compilation – Bruno Kim Feb 15 '18 at 01:21
  • @BrunoKim I do not understand. Java *et al* get their 'view' from symbols in the object code. C and C++ get their 'view' from `#include` files. Where you get 'full view' and 'partial view' is a mystery. They are just two ways of doing the same thing. And object code is still not source code. – user207421 Feb 15 '18 at 09:07
  • I see, I think I have adjusted my mental model now. I had this idea that you'd be able to compile a C file "in isolation", but it indeed has a "full view" of the necessary symbols and their types, just not of their implementations. Thanks for bearing with me. – Bruno Kim Feb 17 '18 at 07:03