Question

i would like to use only certain functions from math.h (WITHOUT including the entire library)

for example, i need to use "sqrt" and "exp", but i have variables named "y1" (and possibly others) which conflict with definitions in math.h

how can i use only certain functions from a library like that?

i tried

#define sqrt cmath::sqrt

but that did not work, i have seen something like that before with

#define cout std::cout

i think, so i thought it might work.

any ideas?

Was it helpful?

Solution

Yes, you can just use the parts you want.

Simply create your own partial mini_cmath.h header for those functions / globals you need access to (assuming those don't conflict!).

As several have noted, there's no way to only #include a given chunk (unless the included header has preprocessor macros to enable such a thing, such as windows.h)

But if you simply declare those functions you wish to use (correctly), and then compile & link (as long as the necessary .lib is included in your link), then you're golden.


However, on a more general note - globals are a bad idea in general, but if you absolutely must use them for hopefully valid reasons, then you should be putting them in a namespace, and referencing them in your source by fully qualified name:

namespace AcmeCorp {
  int g_fubar;
}
AcmeCorp::g_fubar = 9;

OTHER TIPS

Put your code in your own namespace. By using namespace operator (::) you can distinguish variables with the same name (and which are in the same scope).

Just use them?

#include <cmath>

int main()
{
    double d = 4.0;
    sqrt(d);
    exp(d);
}

You can also explicitly specify the namespace:

#include <cmath>

int main()
{
    double d = 4.0;
    std::sqrt(d);
    std::exp(d);
}

You can, if you want, bring in specific names from namespaces without bringing in the whole namespace. This is done using the using keyword. Please don't create a #define for this:

#include <string>

using std::string;

int main()
{
    string s = "foo";
}

Just #include <cmath>. If your variable names are such an issue, then rename them. You can't include just a piece of a file.

EDIT: Why should a y1 conflict with anything in math.h? Use your own namespace. You'll still be able to use sqrt and you can access stuff from your namespace by resolving through mynamespace::y1.

Anyway, cmath is an alias of the header file math.h. std is a namespace, which contains the cout object in iostream.h.

So while you can use std::cout, you can't do the same scope resolution with a header file. You just have to include math.h, or cmath.

And you don't need to use a #define for cout like that. Just add a using namespace std and you won't have to resolve the scope.

And using an entire namespace does not cause any overhead, if that's what you're concerned about. Cheers!

y1() is one of a number of POSIX extensions to the C math library. You can remove these extensions by undefining the following macros before including <cmath>:

#undef _SVID_SOURCE
#undef _BSD_SOURCE
#undef _XOPEN_SOURCE

It's also a very good idea in general to avoid putting any of your own names in the global namespace, since there's a good chance of a collision if you use any C libraries. Avoid global variables whenever you can and, if you must use one, put it inside your own namespace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top