Open source or closed source? That’s a big question. The code you’ve painstakingly written is yours, so naturally you get to decide how to use it. Based on personal preference, everyone is free to choose a suitable way to share or not share. Personally, I don’t think this has anything to do with morality.

But if someone shared their code under the GPL License, and you unilaterally changed the license and made your own code closed source, this is no longer just an ethical issue. Strictly speaking, you have violated the agreement between you and the original author, which is illegal.

In practice, for copyleft or “viral” licenses such as GPLv3, we are required to use the same license for our own code. If someone wants to keep their code closed source, or simply wants to use the MIT License, they cannot just use it directly.

But does this mean an MIT-licensed project can never reference GPL code at all? Those who came before us tell us there are workarounds. The basic principles are:

  • Source-level references and static linking both violate copyright, but you can reference it through a dynamic-link library.
  • A copyright claim requires the rights holder to bring a lawsuit. In other words, if I write some closed-source code and someone steals it, but I don’t go after them, the other party isn’t breaking the law.

So I can first write a wrapper that calls the GPL-licensed program. This wrapper must follow the license requirements of that program. Then my main program only calls the wrapper and does not touch the original GPL content. That way, the only infringement is against myself, and we choose not to sue ourselves…

For example, if some GPL code is packaged as xxx.a with header xxx.h, you can create xxx_wrapper.h and xxx_wrapper.so. xxx_wrapper.h is independent; only xxx_wrapper.cc includes xxx.h, compiled into xxx_wrapper.so, while your project only calls xxx_wrapper.h and xxx_wrapper.so.

In this case, xxx_wrapper.h and xxx_wrapper.cc must still comply with the GPL License. However, linking against xxx_wrapper.so via dynamic linking is, in theory, legal and not bound by the GPL.

One issue in implementing this is that xxx_wrapper.h must not include xxx.h; otherwise it is still invoking the content of the xxx project. So you declare a struct type yourself and define it concretely in the .cc file.

leveldb is an MIT-licensed project that imposes almost no restrictions on users. I still made a wrapper for it to implement some basic functions, though from a copyright perspective this is essentially meaningless. Treat it as a helper.

Header reference here, implementation reference here.

A license is an agreement between people, point-to-point and consensual. I’ve never thought that open-source software is inherently nobler, or that closed-source is especially sinful. My original motivation for making this leveldb wrapper was mainly that directly using leveldb required a bunch of checks every time, so I created a simple macro to bury the complex configuration a bit deeper. But everyone has their own ideas about using leveldb; a set of macros that works for me might for others just add meaningless stack overhead, so this is just to introduce another use for the wrapper.

Moreover, with similar methods you can also construct C-based APIs—that legendary thing of C calling C++.

Postscript,

Open source is a very serious, very sacrificial thing. It is putting your effort into the commons for others to use. When you release it, you should be very clear about what you are licensing to the public.

For example, the GPL License mainly aims for non-commercial use and requires users to feed their improvements back. As a user, you received the code and are restricted from hiding it away to sell; but once you tell everyone about your modifications, you can sell binary releases. My favorite company, Red Hat, sells binaries and services, while CentOS is the result of recompiling the code they release.

BSD grants users the rights to use, modify, and commercialize, but prohibits using the project’s name for advertising.

Apache v2 grants use, commercial modification, and explicitly grants patent rights.

MIT, apart from an explicit statement on patent use, basically says one thing: play with it however you want, modify it however you want, just don’t blame me if you break it.

As a developer, when you open-source something, you should know what you are doing, rather than thinking “Oh my, MIT looks so popular,” and then when someone barely changes it and just packages it for the Apple Store, you feel taken advantage of. Dude, you literally gave it to them.

Not to mention the prehistoric era twenty or thirty years ago—even just ten-plus years ago, when companies were heavily walled off, code was closely guarded, and everyone treasured their own broomsticks, damn it, every company built its own crappy implementation and thought it was hot stuff. It was simply speechless.

It was those idealistic contributors, with the idea of changing the world, who used code and law to carve out a path. In today’s world, even old fossils like Microsoft are learning to share more implementations with the world.

As the fortunate latecomers, I think we should, with full respect, make full use of the wheels our predecessors built, and strive to change the world in return.