I've been stuck at this problem for a while now. I'm currently creating an extension module for Python called simulations. This extension is written in C++. For this extension, I needed to use the gsl library, and link it in my setup.py file. For some reason, if I don't link to the library in libraries, library_dirs, the code doesn't work because it's missing libraries, but if I do, I get the error ImportError: DLL load failed while importing simulations: The specified module could not be found. I'm not sure if they're related at all, but if I run the program without gsl, it works fine.
This is my setup.py:
# Building an extension module from C source
from distutils.core import setup, Extension
import numpy
simulation_mod = Extension('simulations',
sources = ['simulation_main.cpp'],
language="c++",
include_dirs=[numpy.get_include(),
"C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\numpy\\core\\include",
"C:\\cpp_libs\\include",
"C:\\cpp_libs\\include\\bayesopt\\include",
"C:\\src\\vcpkg\\installed\\x64-windows\\include",
"C:\\src\\vcpkg\\installed\\x86-windows\\include",
],
library_dirs=[
"C:\\src\\vcpkg\\installed\\x64-windows\\lib"
],
libraries=[
"gsl",
"gslcblas"
]
)
# The main setup command
setup(name = 'DissertationModels',
version="1.0",
description="Simulation models and temporal integration for different model",
ext_modules=[simulation_mod],
# py_modules=['simulation_interface'],
)
This is my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cpp.exe build active file",
"command": "C:\\msys64\\ucrt64\\bin\\cpp.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-IC:\\src\\vcpkg\\installed\\x64-windows\\include",
"-IC:\\src\\vcpkg\\installed\\x86-windows\\include",
"-IC:\\src\\vcpkg\\installed\\x86-windows\\include\\python3.10",
"-IC:\\Users\\shahi\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\numpy\\core\\include",
"-IC:\\cpp_libs\\include",
"-IC:\\cpp_libs\\include\\bayesopt\\include",
"-LC:\\src\\vcpkg\\installed\\x64-windows\\lib",
"-lgsl",
"-lgslcblas",
"-lm"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-IC:\\src\\vcpkg\\installed\\x64-windows\\include",
"-IC:\\src\\vcpkg\\installed\\x86-windows\\include",
"-IC:\\src\\vcpkg\\installed\\x86-windows\\include\\python3.10",
"-IC:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\numpy\\core\\include",
"-IC:\\cpp_libs\\include",
"-IC:\\cpp_libs\\include\\bayesopt\\include",
"-LC:\\src\\vcpkg\\installed\\x64-windows\\lib",
"-lgsl",
"-lgslcblas",
"-lm"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
and this is my c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${vcpkgRoot}/x64-windows/include", //vcpkgRoot is defined in settings.json
"${vcpkgRoot}/x86-windows/include", //vcpkgRoot is defined in settings.json
"C:/src/vcpkg/installed/x86-windows/include/python3.10", //to get Python.h
"C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\numpy\\core\\include", //numpy
"C:\\cpp_libs\\include", //all cpp_libs
"C:\\cpp_libs\\include\\bayesopt\\include" //bayesopt
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22000.0",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64",
"compilerPath": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
// "configurationProvider": "ms-vscode.cmake-tools"
// "-DCMAKE_TOOLCHAIN_FILE": "C:/src/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
],
"version": 4
}
I get the error after running a python file called wilson_main.py, which has import simulations as sim. I run both py setup.py build and py setup.py install, where I assumed the latter would fix this problem (no errors running either).
My output for setup.py build:
running build
running build_ext
building 'simulations' extension
creating build
creating build\temp.win-amd64-cpython-311
creating build\temp.win-amd64-cpython-311\Release
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\core\include -IC:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\core\include -IC:\cpp_libs\include -IC:\cpp_libs\include\bayesopt\include -IC:\src\vcpkg\installed\x64-windows\include -IC:\src\vcpkg\installed\x86-windows\include -IC:\Users\xxx\AppData\Local\Programs\Python\Python311\include -IC:\Users\xxx\AppData\Local\Programs\Python\Python311\Include "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\ATLMFC\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /EHsc /Tpsimulation_main.cpp /Fobuild\temp.win-amd64-cpython-311\Release\simulation_main.obj
simulation_main.cpp
C:\Users\shahi\AppData\Local\Programs\Python\Python311\Lib\site-packages\numpy\core\include\numpy\npy_1_7_deprecated_api.h(14) : Warning Msg: Using deprecated NumPy API, disable it with #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(101): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(187): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(168): warning C4101: 'kern': unreferenced local variable
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(229): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(230): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(231): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(311): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'unsigned __int64'
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(311): note: consider using '%zd' in the format string
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(311): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 2 has type 'unsigned __int64'
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(311): note: consider using '%zd' in the format string
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(312): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'unsigned __int64'
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(312): note: consider using '%zd' in the format string
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(312): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 2 has type 'unsigned __int64'
C:\Users\shahi\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_helpers.hpp(312): note: consider using '%zd' in the format string
simulation_main.cpp(332): warning C4018: '<': signed/unsigned mismatch
simulation_main.cpp(334): warning C4018: '<': signed/unsigned mismatch
simulation_main.cpp(335): warning C4018: '<': signed/unsigned mismatch
simulation_main.cpp(349): warning C4244: 'argument': conversion from 'npy_intp' to 'int', possible loss of data
simulation_main.cpp(349): warning C4244: 'argument': conversion from 'npy_intp' to 'int', possible loss of data
simulation_main.cpp(358): warning C4018: '<': signed/unsigned mismatch
simulation_main.cpp(360): warning C4018: '<': signed/unsigned mismatch
simulation_main.cpp(361): warning C4018: '<': signed/unsigned mismatch
simulation_main.cpp(374): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'unsigned __int64'
simulation_main.cpp(374): note: consider using '%zd' in the format string
simulation_main.cpp(374): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 2 has type 'unsigned __int64'
simulation_main.cpp(374): note: consider using '%zd' in the format string
simulation_main.cpp(380): warning C4018: '<': signed/unsigned mismatch
simulation_main.cpp(382): warning C4018: '<': signed/unsigned mismatch
simulation_main.cpp(383): warning C4018: '<': signed/unsigned mismatch
simulation_main.cpp(909): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'npy_intp'
simulation_main.cpp(909): note: consider using '%Id' in the format string
simulation_main.cpp(909): warning C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 2 has type 'npy_intp'
simulation_main.cpp(909): note: consider using '%Id' in the format string
creating C:\Users\xxx\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\build\lib.win-amd64-cpython-311
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\bin\HostX86\x64\link.exe" /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\src\vcpkg\installed\x64-windows\lib /LIBPATH:C:\Users\shahi\AppData\Local\Programs\Python\Python311\libs /LIBPATH:C:\Users\xxx\AppData\Local\Programs\Python\Python311 /LIBPATH:C:\Users\xxx\AppData\Local\Programs\Python\Python311\PCbuild\amd64 "/LIBPATH:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\ATLMFC\lib\x64" "/LIBPATH:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22000.0\\um\x64" gsl.lib gslcblas.lib /EXPORT:PyInit_simulations build\temp.win-amd64-cpython-311\Release\simulation_main.obj /OUT:build\lib.win-amd64-cpython-311\simulations.cp311-win_amd64.pyd /IMPLIB:build\temp.win-amd64-cpython-311\Release\simulations.cp311-win_amd64.lib
Creating library build\temp.win-amd64-cpython-311\Release\simulations.cp311-win_amd64.lib and object build\temp.win-amd64-cpython-311\Release\simulations.cp311-win_amd64.exp
Generating code
C:\Users\xxx\OneDrive - Imperial College London\Documents\imperial\Dissertation\Notebooks\MyCodes\models\simulation_main.cpp(287) : warning C4789: buffer 'BOLD_dims' of size 12 bytes will be overrun; 8 bytes will be written starting at offset 8
Finished generating code
and output for setup.py install:
running install
C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\setuptools\command\easy_install.py:144: EasyInstallDeprecationWarning: easy_install command is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running bdist_egg
running egg_info
writing DissertationModels.egg-info\PKG-INFO
writing dependency_links to DissertationModels.egg-info\dependency_links.txt
writing top-level names to DissertationModels.egg-info\top_level.txt
reading manifest file 'DissertationModels.egg-info\SOURCES.txt'
writing manifest file 'DissertationModels.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_ext
creating build\bdist.win-amd64
creating build\bdist.win-amd64\egg
copying build\lib.win-amd64-cpython-311\simulations.cp311-win_amd64.pyd -> build\bdist.win-amd64\egg
creating stub loader for simulations.cp311-win_amd64.pyd
byte-compiling build\bdist.win-amd64\egg\simulations.py to simulations.cpython-311.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying DissertationModels.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying DissertationModels.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying DissertationModels.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying DissertationModels.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
writing build\bdist.win-amd64\egg\EGG-INFO\native_libs.txt
zip_safe flag not set; analyzing archive contents...
__pycache__.simulations.cpython-311: module references __file__
creating 'dist\DissertationModels-1.0-py3.11-win-amd64.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing DissertationModels-1.0-py3.11-win-amd64.egg
removing 'c:\users\xxx\appdata\local\programs\python\python311\lib\site-packages\DissertationModels-1.0-py3.11-win-amd64.egg' (and everything under it)
creating c:\users\xxx\appdata\local\programs\python\python311\lib\site-packages\DissertationModels-1.0-py3.11-win-amd64.egg
Extracting DissertationModels-1.0-py3.11-win-amd64.egg to c:\users\xxx\appdata\local\programs\python\python311\lib\site-packages
DissertationModels 1.0 is already the active version in easy-install.pth
Installed c:\users\xxx\appdata\local\programs\python\python311\lib\site-packages\dissertationmodels-1.0-py3.11-win-amd64.egg
Processing dependencies for DissertationModels==1.0
Finished processing dependencies for DissertationModels==1.0
I've never really built an extension before, so any help is appreciated! :) Thanks.