简体   繁体   English

如何在我的c ++程序中将GDAL库用作可选库?

[英]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.

相关问题 当调用它必须使用的库是导致它崩溃的原因时,如何防止我的 C++ 程序崩溃? - How can I prevent my C++ program from crashing when a call to a library it must use is what causes it to crash? 我可以使用C程序中的C ++模板库吗? - Can I use a C++ template library from a C program? 我可以在C程序中使用C ++创建的共享库吗? - Can I use shared library created in C++ in a C program? 如何在Linux上的C ++程序中使用共享库(在本例中为JsonCpp)? - How do I use a shared library (in this case JsonCpp) in my C++ program on Linux? 如何将标准库静态链接到我的 C++ 程序? - How can I statically link standard library to my C++ program? 如何在我的主程序c ++中使用我自己制作的Array头文件? - How can I use my own made Array header file in my main program c++? C ++:如何使我的程序使用可选的args打开.exe - C++: How to make a my program open a .exe with optional args 我如何获取和使用头文件<graphics.h>在我的 C++ 程序中? - How I can get and use the header file <graphics.h> in my C++ program? 如何在我的 c++ 程序中使用和启动 exe 文件? - How can I use and launch an exe file inside my c++ program? 如何转换我的堆排序程序以使用 C++ 标准容器(例如 std::vector)? - How can I convert my heap sort program to use C++ standard containers (for example std::vector)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM