7

In python, one can import specific feature-sets from different modules, rather than importing the whole file

ex:

Instead of using import math and using print math.sqrt(4), importing the function directly:

from math import sqrt
print sqrt(4)

And it works just fine.


Where as in C and C++, one has to include the whole header file to be able to use just one function that it provides. Such as, in C++

#include<iostream>
#include<cmath>
int main(){
    cout<<sqrt(4);
    return 0;
}

C code will also be similar (not same).


Is it possible that just like as it was in case of python, one can include just one function from a header file into their program?
ex: including just the sqrt() function from cmath?

Could it be done?

zhirzh
  • 3,273
  • 3
  • 25
  • 30
  • @JustinBarber: I thought the OP was asking for a way to do this in C++ and just gave a Python example for reference. – Christian Hackl May 31 '14 at 21:37
  • @Christian Hackl Ah, thank you for that clarification! I was confused by the question and didn't see the C++ tag. Python also has a module named `cmath`. – Justin O Barber May 31 '14 at 21:38
  • @JustinBarber: I guess the OP just accepted some automatically proposed tags and did not realise that his question would be seen by Python people as well as C++ people. – Christian Hackl May 31 '14 at 21:40

1 Answers1

8

No, it is not possible. C++ lacks a true module system, so we are left with preprocessor includes. A proposal to add a new kind of module system did not make it into C++11. See C++ Modules - why were they removed from C++0x? Will they be back later on? for more information on that proposal.

If this is about your own library, your only chance is to split the library into smaller, independent libraries. If the library is not yours and/or you cannot change it, you'll have to live with it. But what's the real problem, anyway?

Community
  • 1
  • 1
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • Well, as per what I've seen, C++ codes on windows machines have an unusually long compile times, but on a unix based system, things are lightning fast.
    i am a windows fan and find it really annoying to have to wait at least 8-10secs, even for the `"hello world"` code to compile. And the main reason are the large header files.
    If one could just include the needed feature, rather than the whole file, things would have been better, I believe.
    – zhirzh May 31 '14 at 21:41
  • @zibs.shirsh: Compile time depends on thousands of things. I don't think the operating-system family plays a signifcant role. There is absolutely nothing inherent in Windows operating systems which would somehow magically slow down all known C++ compilers. – Christian Hackl May 31 '14 at 21:45
  • -like i said, just something I noticed on several machines. May not be a universal fact, but it's not uncommon. – zhirzh May 31 '14 at 21:48
  • @zibs.shirsh: I guess you should try to find the real reason for why compilation of the same code is so much slower on one system. Why don't you post a new question? Make sure to add the whole code (it should be just a few lines for a hello world program), state your exact system specifications, OS and compiler versions (and hardware specs if we are talking about two different machines), show how you invoked the compiler in either case and tell us some actual, measured speed numbers. – Christian Hackl May 31 '14 at 21:48
  • P.S.: Here's a wild guess: you use more complex optimisation options for the Windows compiler. – Christian Hackl May 31 '14 at 21:51
  • usually compilation times are higher on windows on large projects due to the file system, which is slower. On a really big project we had, on similar machines, compiling the same code on windows 7 was taking 90 minutes, while on linux 60. – dau_sama May 31 '14 at 22:50
  • @dau_sama: Both Windows 7 and various Linux versions are not restricted to one type of file system. You mean NTFS, don't you? AFAIK, it depends a lot on the file/directory ratio (i.e. how many files reside in one single directory), but I'm afraid I'm no expert on this. Besides, I thought on really large projects most includes were pulled in from files on network drives, which makes the local filesystem even less important. I am still quite certain that the OP's apparently severe performance problems are NOT due to NTFS. – Christian Hackl Jun 01 '14 at 11:09
  • yes usually NTFS on windows, we have seen a great improvement with the optimizations they've done in 7 (compared to XP), and Windows Server 2012 puts it almost in line with Linux – dau_sama Jun 01 '14 at 11:38