[英]How can I use GDAL library as optional in my c++ program?
I am creating a c++ program that can use GDAL (if present). 我正在创建一个可以使用GDAL(如果存在)的c ++程序。 I can check the presence of GDAL in the system but how I need to code my program to include/not include the GDAL libraries if my program was compiled using GDAL? 我可以检查系统中是否存在GDAL,但是如果我的程序是使用GDAL编译的,我该如何编码我的程序以包括/不包括GDAL库?
Many thanks, Carlos. 非常感谢,卡洛斯。
While I haven't used CMake, but if the library is available you can link with it and also pass a flag to the compiler defining a macro (eg -DHAVE_GDAL
). 虽然我还没有使用过CMake,但是如果该库可用,则可以与其链接,也可以将一个标志传递给定义宏的编译器(例如-DHAVE_GDAL
)。 Then in your source you use the preprocessor to check for HAVE_GDAL
and only use GDAL functionality if it's defined. 然后在您的源代码中使用预处理器检查HAVE_GDAL
并且仅在定义了GDAL功能的情况下使用它。
Something like this in your source: 在您的来源中是这样的:
#ifdef HAVE_GDAL
// Use GDAL functionality
#else
// Use something else
#endif
Generally speaking, if you link your application to a shared object (DLL), your program will fail to start if the shared object is missing. 一般来说,如果将应用程序链接到共享库(DLL),则如果缺少共享库,程序将无法启动。 I'm not sure if there's another way than to simply load the SO on startup, and if you get a handle to the library, use dlsym (or GetProcAddress on Windows) to get hold of the functions you need, and call them that way. 我不确定除了启动时仅加载SO之外,还有其他方法吗?如果您获得了库的句柄,请使用dlsym(或Windows上的GetProcAddress)来获取所需的函数,并以这种方式调用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.