GDAL configuration in windows environment

Table of contents

GDAL source code download

GDAL compilation configuration item parameter modification

GDAL compilation process

Compilation tool selection

compile command

Compile result test

GDAL environment configuration

include

lib

dependencies

dll

Test the introduction of the GDAL library

test code

Test Results

Remark


Reference article:

Download of GDAL2.4.4 and compilation and configuration under VS2015 (Win10)

VS2019 GDAL2.2.4 environment construction

required environment

  1. vs2022

  2. gdal-2.4.4

  3. windows10x64

GDAL source code download

gdal-2.4.4 version download: download link

 Unzip the downloaded compressed package to a custom path. The decompression path for this article is:D:\Packages\gdal-2.4.4

GDAL compilation configuration item parameter modification

Open the downloaded gdal-2.4.4 folder, find the nmake.opt file, and modify the configuration parameters:

1. Save path of compiled gdal

Find line 57, GDAL_HOME = "C:\warmerda\bld" This path is used to store compiled header files, static libraries, dynamic libraries, etc.; the path can be customized and modified here, or D:\Packages\GDAL244not make changes.

2. Line 191, remove #, used to compile GDAL for 64

GDAL compilation process

Compilation tool selection

Open the x64 local tool command prompt under VS2022 and choose to run as an administrator;

 ps: The English version is shown in this article, and the corresponding Chinese version name isVS2022 X64 本机工具命令提示符

Note : The x64-bit native tool command prompt of vs2022 must be selected here, because nmake.optthe option to compile to 64-bit is specified in .

compile command

1. After starting the tool, go to the folder where the decompressed gdal-2.4.4 is locatedD:\Packages\gdal-2.4.4

d:

cd D:\Packages\gdal-2.4.4

2. Compile gdal

Compile GDAL, please pay attention to check your vs version

nmake /f makefile.vc MSVC_VER=1936

Generate bin, html, data folders

nmake /f makefile.vc install MSVC_VER=1936

Generate lib, include folder

nmake /f makefile.vc devinstall MSVC_VER=1936

PS: Determination of MSVC_VER

  • Open vs2022, create a new console project

  • Open main.cpp and fill in the following code

#include <iostream>
using namespace std;

​int main() {
    int num = _MSC_VER; // get the version
    cout << "My MSVC version is: " << num << endl;
    return 0;
 }
  • ctrl+F5 execution, view the output results

Compile result test

GDAL environment configuration

C++ requires two parts to introduce an external library: include and lib. The environment configuration is opened by right- clicking the project-properties .

include

Conventional under C/C++ - additional include directory to fill in the directory where include is located;

lib

In the general-additional library directory under the linker, fill in the directory where lib is located;

dependencies

Fill in Input - Additional Dependencies under Linkergdal_i.lib

dll

        Put the gdal204.dll in the bin library of the compiled gdal into the directory where the executable file .exe of the project is located, otherwise an error will be reported when the program is compiled.

        This article's solution name is ConsoleApplication1named Project ConsoleApplication1. The dll file is placed in the same directory as the executable file:

Test the introduction of the GDAL library

test code

#include<iostream>
#include"stdio.h"
#include<gdal.h>
#include<gdal_priv.h>
using namespace std;

int main()
{
 GDALDataset *poDataset;
 GDALAllRegister();
 poDataset = (GDALDataset*)GDALOpen("D:\\1.tiff", GA_ReadOnly);
 if (poDataset == NULL)
	{
		cout << "文件打开失败!!!";
	}
	else {
		cout << "文件打开成功!!!";
	}
 int nBandCount = poDataset->GetRasterCount();
 int nImgSizeX = poDataset->GetRasterXSize();
 int nImgSizeY = poDataset->GetRasterYSize();   
 
 cout<<nBandCount<<endl<<nImgSizeX<<endl<<nImgSizeY;
 return 0;
 }

Note : D:\1.tiffDocumentation is required. Put a picture on the D drive, and then modify the corresponding file path in the code.

Test Results

 Note : If the compiled gdal cannot import gdal_priv.h, then there is a problem in the process of compiling gdal, which may be caused by an error in using the native tool command prompt.

Remark

The compilation process of other versions of Visual Studio is consistent with vs2022. The key is that the selected native command debugging tool needs to be correct and MSVC_VER needs to be correct.

Guess you like

Origin blog.csdn.net/qq_45929977/article/details/131987015