简体   繁体   中英

Link library.lib using setup.py

I wanted to compile a library to use in my python package using setup.py . The library that I am compiling is installed using vcpkg on windows. During compile I faced this error error LNK2001: unresolved external symbol OGR_GT_Flatten which indicates that I have to link gdal.lib . So I searched a bit and find out that I can do it using two following methods. 1- using /LINK command for cl.exe which I used this command

/link "/LIBPATH:E:\Personal\SideWorks\vcpkg\installed\x86-windows\lib" gdal.lib geos.lib geos_c.lib

and using setup.py file as follwoing

ext_modules = [
    Extension(
        include_dirs=[],
        # extra_link_args=[r'"/LIBPATH:E:\Personal\SideWorks\vcpkg\installed\x86-windows\lib"'],
        libraries =['gdal','geos','geos_c'],
        # library_dirs =['E:\Personal\SideWorks\vcpkg\installed\x86-windows\lib'],
        language='c++'
    ),
]

well I tried both methods but none of them work and the error exists. Here is the command that compiler generates

d:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /nodefaultlib:libucrt.lib ucrt.lib /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO "/LIBPATH:C:\Program Files\Python36\libs" "/LIBPATH:C:\Program Files\Python36\PCbuild\amd64" "/LIBPATH:d:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.7.2\lib\um\x64" "/LIBPATH:D:\Windows Kits\10\lib\10.0.17763.0\ucrt\x64" "/LIBPATH:D:\Windows Kits\10\lib\10.0.17763.0\um\x64" /LIBPATH:E:\Personal\SideWorks\vcpkg\installed\x86-windows\lib gdal.lib geos.lib geos_c.lib /EXPORT:PyInit_pydggrid build\temp.win-amd64-3.6\Release\Personal\Lab\DGGRID\pydggrid-master\src\main.obj build\temp.win-amd64-3.6\Release\Personal\Lab\DGGRID\pydggrid-master\src\lib\dggrid\binpres.obj .
.
.
.
build\temp.win-amd64-3.6\Release\Personal\Lab\DGGRID\pydggrid-master\src\lib\dglib.obj /OUT:build\lib.win-amd64-3.6\pydggrid.cp36-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.6\Release\Personal\Lab\DGGRID\pydggrid-master\src\pydggrid.cp36-win_amd64.lib
Creating library build\temp.win-amd64-3.6\Release\Personal\Lab\DGGRID\pydggrid-master\src\pydggrid.cp36-win_amd64.lib and object build\temp.win-amd64-3.6\Release\Personal\Lab\DGGRID\pydggrid-master\src\pydggrid.cp36-win_amd64.exp
gridgen.obj : error LNK2001: unresolved external symbol GDALAllRegister
quadclip.obj : error LNK2001: unresolved external symbol CSLPartialFindString
DgOutShapefile.obj : error LNK2001: unresolved external symbol DBFWriteStringAttribute
build\lib.win-amd64-3.6\pydggrid.cp36-win_amd64.pyd : fatal error LNK1120: 215 unresolved externals
error: command 'd:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\bin\\HostX86\\x64\\link.exe' failed with exit status 1120

well it shows that the link command is added to linker.exe but I still get the same error. I checked and libraries (*.lib) files exist in that directory. Where am I doing wrong that this error appears? Thanks

While this is an old question, I guess (and hope) some people might benefit from an answer.

When I faced the same problem, I managed to provide my .lib files with extra_objects :

ext_modules = [
    Extension(
        include_dirs=[],
        extra_objects=[
            r'E:\Personal\SideWorks\vcpkg\installed\x86-windows\lib\gdal.lib',
            r'E:\Personal\SideWorks\vcpkg\installed\x86-windows\lib\geos.lib',
            r'E:\Personal\SideWorks\vcpkg\installed\x86-windows\lib\geos_c.lib'
        ]
        language='c++'
    ),
]

(I don't know if the path's are correct, but you get the idea.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM