Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I include/import a single function from a library in C++

I only need to use the iequals case-insensitive string comparison function from the BOOST library.

I am using #include <boost/algorithm/string.hpp> to import it.

Is there any way that I could only import the iequals function by itself?

The reason I even care (I am really just curious.) is because the compiled DLL is about 230kB if I don't #include it and about 1.1MB if I do. It doesn't really make much difference in this particular case how large the file is, but it seems like there is a lot of stuff that gets imported and never used. What if the library was a few GB and I only needed one of the functions? Then it would become an issue, I imagine.

I am admittedly naive when it comes to just about anything cpp-related, but I feel like it isn't very efficient to include some 750kB of code when probably 90% of it isn't used. It could be that the iequals function uses all of that 750kB, I have no idea.

Then again, if the iequals function includes many of the same libraries, the file would still be just as large.

Thoughts?

Thanks in advance for any advice.

EDIT:

Thanks for the responses. I am doing my best to understand them.

I am a chemical engineer who is rewriting a bunch of horribly slow and poorly optimized VBA macros into a C++ DLL. So far the results have been outstanding and everything functions correctly. I just don't see the need for the extra file size if I only need to do a single type of comparison between two strings.

An example of the comparison I need to do is as follows:

if (SomeBSTR == "SomeTextHere") {
    // do stuff
}

or more exactly:

if (Gas == "Methane"    or
    Gas == "CH4"        or
    Gas == "C1")        return 1;

if (Gas == "Ethane"     or
    Gas == "C2H6"       or
    Gas == "C2")        return 2;

If this is the ONLY type of comparison that I have to do, can I do it in a more simple way than:

int wStrCmp(const BSTR Str1, const wstring Str2) {

    wstring wStr1(Str1, SysStringLen(Str1));

    return boost::iequals(Str1, Str2);
}

which is called via:

if (wStrCmp(Gas, L"Methane")      or
    wStrCmp(Gas, L"CH4")          or
    wStrCmp(Gas, L"C1"))          return 1; 

Those last 2 blocks are practically pasted from my code.

Thanks again, guys.

like image 696
Dr3vvn45ty Avatar asked Oct 26 '12 18:10

Dr3vvn45ty


Video Answer


1 Answers

believe me you already just include boost::algorithm::iequals but it use boost::range and std::locale that possibly you don't use them in other places of your code, so this make your code a lot bigger, so I guess for your case there is no other way to do that unless you use some non-standard function like stricmp or strcasecmp.

If you want to compare wide strings on Windows(for example BSTR) you can use _wcsicmp from CRT or lstrcmpiW from Windows runtime(declared in Kernel32.lib that possibly you already linked with it).

like image 193
BigBoss Avatar answered Sep 19 '22 20:09

BigBoss