CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2012
    Location
    Ghent, Belgium
    Posts
    4

    Only compile code when library is found

    Hello,

    For the past few months I've been working with a lot of different libraries. During this process, I have written some classes & functions that I would like to keep. I have put all of these in my own static library (PJLib). Because some of those libraries (HALCON, ORiN2, ...) are of commercial use, I won't have access to them anymore at the end of this month. Is there any possibility that I can exclude the classes & functions that depend on these libraries from the build of PJLib, in the case that they are not found on the disk? This way, I can keep PJLib up to date without having to keep different versions. It is quite likely that I will have access to HALCON again very soon, so I don't want to throw out the code. It will also make it easier to share my code with others ...

    Thanks a lot!

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Only compile code when library is found

    If you are actually wanting to COMPILE code depending on whether certain libraries are present, then there really is only one solution for this. And that is to use the #ifdef / #endif type preprocessor stuff.
    It does mean your users will need to somewhere enable/disable the matching #defines, so centralizing this in a single header file is recommended.

    If you are talking about making your application, but having it make use of certain proprietary/commercial DLLs that may or may not be present on the user machines, that's a different story entirely.

  3. #3
    Join Date
    Jun 2012
    Location
    Ghent, Belgium
    Posts
    4

    Re: Only compile code when library is found

    Quote Originally Posted by OReubens View Post
    If you are talking about making your application, but having it make use of certain proprietary/commercial DLLs that may or may not be present on the user machines, that's a different story entirely.
    Ok, I really don't know what I'm talking about, so I hope to find a starting point here. What I want to do is still include the same static library as I always do in my projects, but when certain libraries (that are used by this library) are not found, I want the compiler to ignore the functions/classes that use these. So that I can build my project anyway ... (without making use of those functions of course)

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Only compile code when library is found

    if you talk about actual code libraries, then we're talking about including code in the executable image.
    This can only be done by #ifdef preprocesor instructions.
    There is NO way you can make the compiler "detect" which libraries are present, because the compiler does not know this.

    If you talk about the simple import libraries, then this is basically the same deal. you include code that will make sure a DLL gets loaded alongside yoru own executable, and that all the links to this DLL get matched up.
    If this is what you talk about, then the solution is to either use implicit loading of DLL's via LoadLibrary/GetProcAddress. And this will mean you need to do some extra work to get this to function.

    Alternatively, you can use a semi-automated process called DelayLoading, but you'll also need to put some code in place to handle the cases where the expected DLL's aren't present. This may take some work since this isn't what the delayloading system was designed to do (although there are provisions for it in so far as you provide your own fallbacks).

    For what you are asking, the LoadLibrary/GetProcAddress is the easier solution of the two.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Only compile code when library is found

    THe reason you can't solve this "in the compiler":

    Suppose a library isn't present. What is the compiler going to do with a call being made to a function in that library ? What is the expected result of that call not being present. Should the compiler give a warning message and abort your program ? Or return zero ? or return an errornumber ? or ?


    At the very least, you'll somehow need to provide "fallbacks" for every function in the libraries in the case the libraries aren't there. THis could be disabling some menu items, this could be returning "dummy" solutions, this could be messages....

  6. #6
    Join Date
    Jun 2012
    Location
    Ghent, Belgium
    Posts
    4

    Re: Only compile code when library is found

    Aha, I think I understand!

    So I should encapsulate the library-dependent parts with #ifdef's, and then manually set those?

    The reason I thought there might be a simple (automatic) way to do this, was because when using CMake to build bigger projects, it first checks whether it finds the needed libraries on the system ...

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Only compile code when library is found

    There are many ways you could "encapsulate" the technology behind it. And you may be able to use pre-compile steps to do the figuring out for you and generating code/header files doing so, in a way this is going to be what that CMake setup is going to do. They probably have a source file for "if library X is available" and another one "if library X isn't available", and then depending on presense include one or the other in the compilation and liking steps. This would not use #ifdef's it just compiles different CPP's depending on need.

    At the core of it all however, it boils down to having some bit of c++ code that takes care of the case with and the case without the library.

    YOU need to provide the code what to do for both cases.

  8. #8
    Join Date
    Jun 2012
    Location
    Ghent, Belgium
    Posts
    4

    Re: Only compile code when library is found

    Mh yeah ... This stuff is getting really confusing for me. I just wanted the code to be left out of my own Library (PJLib) when the 3D parties aren't found. Then this code would not be able to be used in any program making use of PJLib, and I would not need to provide an alternative.

    I guess this is a little too complicated for me right now so I might just split up my library in a few parts anyway and go from there.

    Thanks for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured