0

I have a program (cpp) with many classes. Every class is in separate source file (.h + .cpp).
How can I split the compiled program into multiple files (instead of one big executable file)?
Let's say, one file for every class (same as the code structure).
So that every time there is change in a specific class, I compile only that class, and replace the specific compiled file related to that class.
(Something similar to .DLL files in Windows.)

Example from real life:
I am making TUI interface for managing mysql.
I would like to create mysql text editor (TUI) with ncurses.

the code (class) for creating and managing single window object is in 'textWin.cpp' + 'textWin.h'

the code (class) for managing multiple windows, by creating windows objects from previous class is in winMan.cpp winMan.h

the code (class) for managing mysql database is in : mysql.cpp mysql.h

and so on...

so, I have the following files:
MyProgram.cpp
- winMan.cpp + winMan.h
- textWin.cpp + textWin.h
- mysql.cpp + mysql.h
- ..
- ..

After g++ compilation, I get one executable file, './MyProgram' (size about 15Mb.) which I deliver to all my customers (1000's of them).

I Just found a typo in textWin.cpp, I fixed it, and I told to all customers that there is an update... all of them need to download one big 15Mb file, this consumes allot of bandwidth and server resources, for just a small update.
Is there a way to send to all my customers smaller file, that contains only the compiled code for textWin class ?

I use g++ on Centos7

orenk
  • 109
  • 1
  • 2
  • 12
  • 4
    Shared libraries (.so) are the equivalent of DLLs in Linux. You can divide your application into multiple .so files based on the level of granularity that works for you. – R Sahu Feb 18 '20 at 19:16
  • 2
    FWIW, 15MB is not what I would call big. That's actually small for an "update". – NathanOliver Feb 18 '20 at 19:17
  • It's not a debug build, right? – HolyBlackCat Feb 18 '20 at 19:18
  • Bah. When I was a lad, we had 2K program memory. An' we were damn glad to have that. Online update was a pipe dream. We had to walk up the side of a mountain to to reprogram the EEPROMS. Forty miles. Uphill. Both ways. – user4581301 Feb 18 '20 at 19:34
  • Supposing that some of your classes use others of your classes, it is likely that changes to one class will often require multiple sources to be rebuilt. – John Bollinger Feb 18 '20 at 19:55
  • @RSahu Thanks.. This is what I was looking for. – orenk Feb 18 '20 at 20:24

3 Answers3

0

Of course, you could put your texts into separate text-files and only deploy those in the an error is there. For your special use case, where binary differences must be deployed, this question might be helpful: How do I create binary patches?

Another option, do proper versioning. That way, your customers might be able to decide for themselves. That is, if they need this update.

Marc Barbeau
  • 826
  • 10
  • 21
  • Thanks for the answer. changing Text was only example... sometime its new feature or bug fix. – orenk Feb 18 '20 at 20:15
0

The gcc compiler will happily take a list of cpp files to compile together to make one executable. You don't need to write a "containing" cpp file. However, you still have the issue that each time it rebuilds them all.

The alternative is to build each sourcefile separately to an object file, then link those all together. Hopefully each of those invocations of the compiler will add up to less time than the single command-line. But how to keep track of which cpp files actually need to be rebuilt?

The usual approach is to use a makefile and a make utility which will check the dates of all the mentioned files. There are a variety of flavours of makefile, and helper makefile engines. Download a simple package like gzip and you can quickly get an idea of how the Makefile is structured. Then there is lots of help online, or you may decide that this is just too much trouble for a project with 5 files in it.

Gem Taylor
  • 5,381
  • 1
  • 9
  • 27
0

As suggested in the comments by @RSahu
Shared Libraries (.so files) is the way to split your compiled code.

here is a small example:
https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

orenk
  • 109
  • 1
  • 2
  • 12