简体   繁体   中英

C++ Avoiding library linking

I currently have a c++ setup like the following

class FlowController
{
   public:
    //...
   private:
       cntrl::OneWayValve _intake;
}

As you can see i'm using a cntrl::OneWayValve instance within my class. The Valve class resides in another library which i link with at compile time. The cntrl::OneWayValve has a cntrl::Value within its implementation like so.

class OneWayValve
{
   public:
    //...
   private:
       cntrl::Valve _valve;
}

And as before the cntrl::Valve resides in a different library for reasons you'll have to ask the previous developer about.

Now when i compile my FlowController class i'm required to link with the OneWayValve library and the cntrl::Valve library as well.

My question: Is it possible to only link with the cntrl::OneWayValve library at compile time?

Forward declaration?
Static libraries (really don't want to do this tho)?
Another alternative?

Basically i don't want to know that its using a cntrl::Valve internally, its none of my business.

Note: apologies the OS is Unix.

Cheers, Ben

What you could do is make your Valve library part of your OneWayValve library using a tool called a librarian. I don't know what OS/compiler you are using so I'm going to describe how do it using Visual Studio since that's the only system I've actually done this with (unless you want to count CP/M + LIB-80 :-)

If you bring up the Tools|Options dialog for you OneWayValve project and select Configuration Properties|Librarian|Additional Dependencies, you can put a reference to your Valve library in the Additional Dependencies setting. This will cause OneWayValve.lib to contain any objects that it references from Valve.lib.

Unfortunately for you, the OneWayValve isn't very well designed. Not only you need to link to both libraries, but you will also have to recompile both the OneWayValve library and your code if the Valve class changes.

You can do it by defining all methods of OneWayValve and Valve in their headers as inline. Then you don't need to link to the library.

But if it was designed that way, then what problems are linking to this library causing? Nothing wrong with dynamically linking a library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM