[gdal-dev] How to Use Gdal in Microsoft Visual Studio C++

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

[gdal-dev] How to Use Gdal in Microsoft Visual Studio C++

Athin
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Mateusz Loskot
On Wed, 31 Oct 2018 at 02:41, Athin <[hidden email]> wrote:
>
> I am still new in this GIS subject. May someone guide me how can i use gdal
> on Visual Studio 2012 c++.

In order to build and use current GDAL, you need Visual Studio 2015 or later.
GDAL requires C++11 compiler
https://trac.osgeo.org/gdal/wiki/rfc68_cplusplus11

You may try VS2013, which offers some C++11 support,
but it is not guaranteed to work at all.

> I try to follow from the website Gdal.org but i
> dont understand about it. How to generating visual studio project? How to
> generate_vcxproj.bat?

For example,

D:\gdal\gdal\> generate_vcxproj.bat 15.0 64 gdal_vs2017
Generating:
  "D:\gdal\gdal\gdal_vs2017.vcxproj"
  "D:\gdal\gdal\gdal_vs2017.vcxproj.user"
  "D:\gdal\gdal\gdal_vs2017.vcxproj.filters"
  "D:\gdal\autotest\cpp\gdal_vs2017_test.vcxproj"
  "D:\gdal\autotest\cpp\gdal_vs2017_test.vcxproj.user"
  "D:\gdal\autotest\cpp\gdal_vs2017_test.vcxproj.filters"
This might take a little while...
Projects done!
Launch Visual Studio IDE
* Open project "D:\gdal\gdal\gdal_vs2017.vcxproj"
* Add  project "D:\gdal\autotest\cpp\gdal_vs2017_test.vcxproj"
* Configure Build Dependencies to build the main project before the
test project.
* Save solution in .sln file
Done!

Finally, since you asked " guide me how can i use gdal",
you do not need VS project to build or use GDAL.
You can build GDAL library from command line using NMAKE and Makefiles provided
See https://trac.osgeo.org/gdal/wiki/BuildingOnWindows
(Note, the page has not been updated regarding minimum VS version
required, so assume it is VS2015).
Then, create your own application project in VS and configure it
setting include directories,
library directories, libraries for linker input.
If you don't know how to use VS, you will need to learn it from the
Microsoft docs, eg.
https://docs.microsoft.com/en-us/cpp/build/vscpp-step-1-create

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Ivan Lucena-5
Hi Athin,

Just to complement what Mateusz wrote, the project files generated automatically are not a *real* Visual Studio C++ project that you can change parameters on the project's property dialogbox. The generated project is what Microsoft calls a Makefile project. It is just a shell around the existing makefile.vc files. When you click on build all it does is to run "name -f makefile.vc" with some parameters.

So it might not be a good idea if you need to integrate with a VS Solution that need to share settings with other *normal* VC projects. The other options for that would be 'GDAL cmake' [https://trac.osgeo.org/gdal/wiki/CMake] but I never use it.

If all you want to do is to compile GDAL I think you would be better off by using command line tools (edit nmake.local based on nmake.opt and run nmake -f makefile.vc, make, make install). Just like Mateusz said.

The automaticly generated projects would give you a complete code scan with autocompletion, go to declaration, go to definition and refactoring tools, just like in a *normal* Visual Studio project but you still need to edit nmake.local to change setting like debugging mode, for example. And more importantly, I haven't use it for a long time so I almost forgot that you can use it for debugging too.

Good luck!

Ivan



On 10/31/18, 4:57 AM, "gdal-dev on behalf of Mateusz Loskot" <[hidden email] on behalf of [hidden email]> wrote:

    On Wed, 31 Oct 2018 at 02:41, Athin <[hidden email]> wrote:
    >
    > I am still new in this GIS subject. May someone guide me how can i use gdal
    > on Visual Studio 2012 c++.
   
    In order to build and use current GDAL, you need Visual Studio 2015 or later.
    GDAL requires C++11 compiler
    https://trac.osgeo.org/gdal/wiki/rfc68_cplusplus11
   
    You may try VS2013, which offers some C++11 support,
    but it is not guaranteed to work at all.
   
    > I try to follow from the website Gdal.org but i
    > dont understand about it. How to generating visual studio project? How to
    > generate_vcxproj.bat?
   
    For example,
   
    D:\gdal\gdal\> generate_vcxproj.bat 15.0 64 gdal_vs2017
    Generating:
      "D:\gdal\gdal\gdal_vs2017.vcxproj"
      "D:\gdal\gdal\gdal_vs2017.vcxproj.user"
      "D:\gdal\gdal\gdal_vs2017.vcxproj.filters"
      "D:\gdal\autotest\cpp\gdal_vs2017_test.vcxproj"
      "D:\gdal\autotest\cpp\gdal_vs2017_test.vcxproj.user"
      "D:\gdal\autotest\cpp\gdal_vs2017_test.vcxproj.filters"
    This might take a little while...
    Projects done!
    Launch Visual Studio IDE
    * Open project "D:\gdal\gdal\gdal_vs2017.vcxproj"
    * Add  project "D:\gdal\autotest\cpp\gdal_vs2017_test.vcxproj"
    * Configure Build Dependencies to build the main project before the
    test project.
    * Save solution in .sln file
    Done!
   
    Finally, since you asked " guide me how can i use gdal",
    you do not need VS project to build or use GDAL.
    You can build GDAL library from command line using NMAKE and Makefiles provided
    See https://trac.osgeo.org/gdal/wiki/BuildingOnWindows
    (Note, the page has not been updated regarding minimum VS version
    required, so assume it is VS2015).
    Then, create your own application project in VS and configure it
    setting include directories,
    library directories, libraries for linker input.
    If you don't know how to use VS, you will need to learn it from the
    Microsoft docs, eg.
    https://docs.microsoft.com/en-us/cpp/build/vscpp-step-1-create
   
    Best regards,
    --
    Mateusz Loskot, http://mateusz.loskot.net
    _______________________________________________
    gdal-dev mailing list
    [hidden email]
    https://lists.osgeo.org/mailman/listinfo/gdal-dev

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Athin
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Athin
In reply to this post by Athin
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

bradh
The problem below is nothing to do with GDAL, you just need to initialise the variable (probably by some kind of command line argument - argv[1] if nothing else). If that doesn't make sense, is there another programming language that you are more comfortable with?

Brad

-----Original Message-----
From: gdal-dev <[hidden email]> On Behalf Of Athin
Sent: Thursday, 1 November 2018 8:32 PM
To: [hidden email]
Subject: Re: [gdal-dev] How to Use Gdal in Microsoft Visual Studio C++

Hai All,

My target just know want to try to display raster map, but i still stuck in this starting point. The code I run below will display an error

error c4700 uninitialized local variable 'pszFilename' used

can someone help me regarding this and what i need to with the file i download from this link:https://trac.osgeo.org/gdal/wiki/DownloadSource

where i need to add on VS2012?

Thank you and best regards

#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc() #include "stdafx.h"
#include "gdal.h"


int main()
{
        class GDALDataset;
        const char *pszFilename;
       

    GDALDataset *poDataset;
        void CPL_DLL CPL_STDCALL GDALAllRegister(void);

    poDataset = (GDALDataset *) GDALOpen(pszFilename, GA_ReadOnly );
    if( poDataset == NULL )
        {}

}



--
Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Ivan Lucena-5
In reply to this post by Athin
Athin,

If this is your target, GDAL might not be the right choice for you.
   
    My target just know want to try to display raster map, but i still stuck in
    this starting point. The code I run below will display an error

The GDAL API doesn't support display capabilities. If you go ahead and solve the problems that you are facing you will end up with a buffer of pixels in memory, in order to get that as an image/map into a desktop window you need to write your own code (against Microsoft APIs probably) or call some other library that have that capability.
   
    can someone help me regarding this and what i need to with the file i
    download from this link:https://trac.osgeo.org/gdal/wiki/DownloadSource

You don't need to download the GDAL source code and compile it to follow the GDAL tutorial. You can get the binary distribution from GISInternals [https://trac.osgeo.org/gdal/wiki/DownloadingGdalBinaries]
   
    where i need to add on VS2012?
    error c4700 uninitialized local variable 'pszFilename' used

I would advise you to take some VS and C++ tutorials/exercises to get familiar with the language itself, error messages and the programming environment.

Good luck,

Ivan

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Athin
In reply to this post by bradh
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Athin
In reply to this post by Ivan Lucena-5
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

bradh
In reply to this post by Athin
There are C# bindings for GDAL: https://trac.osgeo.org/gdal/wiki/GdalOgrInCsharp
Examples are at https://trac.osgeo.org/gdal/browser/trunk/gdal/swig/csharp/apps

You may also find Nuget packages, however I haven' tried that.

Brad

-----Original Message-----
From: gdal-dev <[hidden email]> On Behalf Of Athin
Sent: Friday, 2 November 2018 11:44 AM
To: [hidden email]
Subject: Re: [gdal-dev] How to Use Gdal in Microsoft Visual Studio C++

Hai Bradh,

Sorry I not use to C++, I only familiar with C#. I need to use C++ because Gdal support it and i need to combine it to the Visual Studio C# (Wpf).

Thank you and best regards.



--
Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Ivan Lucena-5
In reply to this post by Athin
Hi Athin,

I think we have some problem with the GDAL tutorials. The first example in C and C++ have the same error as your code, pszFilename is not initialized [https://www.gdal.org/gdal_tutorial.html]. It is not supposed to be a full source code sample, but anyway...

    Yeah I will take the C++ tutorials/exercise. But I need to try a bit the
    Gdal capability. I just want to try it first hand by follow the tutorial,
    but it seem there is not many example that allow to see the running of
    complete code. Did you have any GDAL sample that can share with me?

The code that I wrote for GDAL is for format-drivers (BYN, RST, GEORASTER, INGR) and GDAL driver code are usually too complex and not good example, if what you want is to developer application based on the GDAL public API.

But we do have some good code sample in other programming language that might be of you interest:

https://github.com/OSGeo/gdal/tree/master/gdal/swig/csharp/apps

https://github.com/OSGeo/gdal/tree/master/gdal/swig/java/apps

https://github.com/OSGeo/gdal/tree/master/gdal/swig/python/samples

Best regards,

Ivan







_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Athin
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Ivan Lucena-5
Hi Athin,

Here it goes again:

https://github.com/OSGeo/gdal/tree/master/gdal/swig/csharp/apps

For more information on GDAL's C# API:

https://trac.osgeo.org/gdal/wiki/GdalOgrInCsharp

Regards,

Ivan

On 11/6/18, 8:34 PM, "gdal-dev on behalf of Athin" <[hidden email] on behalf of [hidden email]> wrote:

    Hai Ivan,
   
    May i know the function of the swig(c#) that you share to me?
   
    Thank you and best regards
   
   
   
    --
    Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
    _______________________________________________
    gdal-dev mailing list
    [hidden email]
    https://lists.osgeo.org/mailman/listinfo/gdal-dev

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Tamas Szekeres
Packages for GDAL have also been submitted to nuget.org  (GDAL, GDAL.Native and GDAL.Plugins) which could be referenced in your Visual Studio project easily.


Best regards,

Tamas

Ivan Lucena <[hidden email]> ezt írta (időpont: 2018. nov. 7., Sze, 14:44):
Hi Athin,

Here it goes again:

https://github.com/OSGeo/gdal/tree/master/gdal/swig/csharp/apps

For more information on GDAL's C# API:

https://trac.osgeo.org/gdal/wiki/GdalOgrInCsharp

Regards,

Ivan

On 11/6/18, 8:34 PM, "gdal-dev on behalf of Athin" <[hidden email] on behalf of [hidden email]> wrote:

    Hai Ivan,

    May i know the function of the swig(c#) that you share to me?

    Thank you and best regards



    --
    Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
    _______________________________________________
    gdal-dev mailing list
    [hidden email]
    https://lists.osgeo.org/mailman/listinfo/gdal-dev

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev

_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Athin
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Gigas002
Hi Athin,
Not exactly an answer to your question, but there is a good nuget package
for using in c#, called GDAL.NET:
https://www.nuget.org/packages/GDAL.NET/
It contains bindings, automatically added to your project after build gdal's
binary files and GdalConfiguration class that will automatically setup
GDAL/OGR environment variables and paths after you call ConfigureGdal().
About covering of .net gdal, I suppose bindings have been a bit forgotten in
community, and there some really outdated classes, that have only pre-2.0
funcs (take a look at this issue for example):
https://github.com/OSGeo/gdal/issues/918
As for me, I personally use GDAL.NET bindings as a replacement for gdal
utilities (gdal_translate, gdalwarp, gdalbuildvrt etc.), I just prefer to
call them as methods from code, not as process.



--
Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Athin
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to Use Gdal in Microsoft Visual Studio C++

Gigas002
Athin wrote
> If possible can you share with some guide how to use the gdal nuget and
> how
> can i view the map on wpf not on console?

Sure. You should right-click on "References" in your open project in VS and
choose "Manage nuget packages". Then search for "GDAL.NET" and install it.
Necessary references and GdalConfigurator class will automatically be added
to your project.

If you don't have internet on your PC to download nuget packages directly
from VS, you can download GDAL.NET package from nuget:
https://www.nuget.org/packages/GDAL.NET/. Then, you should change you nuget
search path, here's good answer on stackoverflow how to do it:
https://stackoverflow.com/questions/10240029/how-do-i-install-a-nuget-package-nupkg-file-locally.

After you build your project once, compiled gdal bindings will be placed in
your dubug/release directory near .exe file.

Now you should call "GdalConfigurator.ConfigureGdal()" (or ConfigureOgr(),
depends on what you need) method in you program. If it doesn't throw any
exceptions, then gdal is found and all EnvironmentVariables set correctly.
If it throws exceptions, try changing solution platform on x64 and change
the following line in GdalConfiguration class from:

var executingAssemblyFile = new
Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;

to

string executingAssemblyFile = Assembly.GetExecutingAssembly().Location;

The thing is, that original version works bad, if path contains some special
symbols, like "#", etc.

After you made your ConfigureGdal() method work correctly, you should add
"using OSGeo.*" namespaces to your code files, and now you can use all
gdal's c# bindings.

Unfortunately, I can't answer to your second question, I haven't done it
before. If I'll have time, I'll check it out and write you about results.



--
Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
_______________________________________________
gdal-dev mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/gdal-dev