Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to reference .dll in Visual Studio #17293

Open
1 task done
zxuan99 opened this issue Nov 8, 2024 · 12 comments
Open
1 task done

[question] How to reference .dll in Visual Studio #17293

zxuan99 opened this issue Nov 8, 2024 · 12 comments
Assignees

Comments

@zxuan99
Copy link

zxuan99 commented Nov 8, 2024

What is your question?

Hi, I am using a pre-built library binary that I have uploaded into my private conan server and my application is using that . After I uploaded it, I have a runtime error and my application shows an error that "uhd.dll" was not found. But I have already added the following statement in my Conanfile.py in the library and the uhd.dll is indeed in the application's build folder under "C:\Users\user.conan2\p\b\uhdf0f5441a1bd05\p\bin" as I have cross checked it after building my application.

def package(self):
copy(self, "*.dll", bin_folder , os.path.join(self.package_folder, "bin"), keep_path=False)

By the way, I am using Visual Studio. May I ask what do I short of?

Have you read the CONTRIBUTING guide?

  • I've read the CONTRIBUTING guide
@memsharded memsharded self-assigned this Nov 9, 2024
@memsharded
Copy link
Member

Hi @zxuan99

Conan uses packages with some "generators", for example it uses the CMakeDeps generator to generate some .cmake files that help locating and using the dependencies for "build" time, to find the headers and link with the libraries. For other build system, when not using CMake, there is MSBuildDeps, PkgConfigDeps, etc. Which build system are you using in the consumer side?

Then, for runtime dependencies, Conan uses the VirtualRunEnvironment, that it will generate a conanrun.bat file. Activating this file, by running it in the terminal before running the application, will define the PATH environment variable, so the dependencies .dll files can be found.

For IDEs, there are some generators that provide some support, for example CMakeToolchain define the variable CMAKE_VS_DEBUGGER_ENVIRONMENT that should take care of this.

Can you please report the full consumer conanfile, the exact conan install commands and the output, and the build system steps, like cmake ... if using CMake, or just specify that you open the IDE, etc. Thanks for your feedback.

@zxuan99
Copy link
Author

zxuan99 commented Nov 11, 2024

Hi @memsharded

Thanks for the reply and I appreciate it. As mentioned, the library that I am uploading to the conan server is pre-built so there is no build steps required. I am using Visual Studio so the build system is MSBuild.

Please refer to the following for the conanfile.py.

import os
from conan.tools.files import get, copy
from conan import ConanFile

required_conan_version = ">=1.55.0"

class UHDConan(ConanFile):
name = "uhd"
version = "3.15.0.0"
url = "https://files.ettus.com/binaries/uhd/uhd_003.015.000.000-release/Windows-10-x64"
description = "The UHD (USRP Hardware Driver) library is a software framework developed by Ettus Research, primarily used for controlling and interacting with USRP (Universal Software Radio Peripheral) hardware devices."
settings = "os", "arch"
no_copy_source = True

def layout(self):
    self.cpp.source.includedirs = ["include"]
    self.cpp.build.libdirs = ["lib"]

def package(self):
    include_folder = os.path.join(self.recipe_folder, "include")
    lib_folder = os.path.join(self.recipe_folder, "lib")
    bin_folder = os.path.join(self.recipe_folder, "bin")

    copy(self, "*.h", include_folder , os.path.join(self.package_folder, "include"), keep_path=False)
    copy(self, "*.hpp", include_folder , os.path.join(self.package_folder, "include"), keep_path=True)
    copy(self, "*.lib", lib_folder , os.path.join(self.package_folder, "lib"), keep_path=False)
    copy(self, "*.dll", bin_folder , os.path.join(self.package_folder, "bin"), keep_path=False)

def validate(self):
    if self.settings.os != "Windows":
        raise ConanInvalidConfiguration("Only Windows supported")
    if self.settings.arch not in ("x86", "x86_64"):
        raise ConanInvalidConfiguration("Unsupported architecture")

def package_info(self):
    self.runenv_info.append_path("PATH", os.path.join(self.package_folder, "bin"))
    self.cpp_info.includedirs = ["include"]
    self.cpp_info.libdirs = ["lib"]
    self.cpp_info.libs = ["uhd"]

@memsharded
Copy link
Member

Can you please report the full consumer conanfile,

I meant, the conanfile you are using to consume that package, not the conanfile used to produce the package.

In the producer conanfile.py you shared, there are some points that could be improved (I am assuming you are using Conan 2, bw):

  • The self.runenv_info.append_path("PATH" is unnecessary, it happens by default
  • The self.cpp_info.includedirs = ["include"] is also the default, not necessary to specify
  • The self.cpp_info.libdirs = ["lib"] is also the default not necessary to specify
  • The package() method is generally not expressed in terms of self.recipe_folder, but in terms of self.source_folder and self.build_folder, which are defined in the layout() method, so they can also be parameterized for working locally as editable if necessary, with different settings

Then, the important information needed is:

  • The consumer conanfile
  • The commands used to install from the consumer conanfile
  • The output of the commands used, and depending on the generators used, the output of those generators
  • If you are using pure MSBuild, note that you need to inject the generated files from MSBuildDeps and MSBuildToolchain into your VS project

@zxuan99
Copy link
Author

zxuan99 commented Nov 13, 2024

Hi,

Sorry, I am a new Conan user and I do not quite get what you mean by consumer/producer conanfile. All I am trying to do is to create a package to upload to my private conan server so that it will automatically be linked and used in my Visual Studio to build it. For context, I have a library files with pre-built binary and I am trying to turn the package into a Conan library. Based on what you have mentioned, it seems like the Conanfile.py that I have written is wrong? Appreciate your advice on this.

Thanks

@memsharded
Copy link
Member

Sorry, I am a new Conan user and I do not quite get what you mean by consumer/producer conanfile. All I am trying to do is to create a package to upload to my private conan server so that it will automatically be linked and used in my Visual Studio to build it. For context, I have a library files with pre-built binary and I am trying to turn the package into a Conan library. Based on what you have mentioned, it seems like the Conanfile.py that I have written is wrong? Appreciate your advice on this.

The producer is the conanfile.py of your package. The one you do conan create <path/to/my/conanfile.py> or conan export-pkg <path/to/my/conanfile.py>.
Your conanfile.py doesn't look completely wrong, just some points for improvement described above.

The consumer is the conanfile.py, or conanfile.txt that contains a self.requires() or a [requires] to your created package, to use the package. It must declare the right generators like CMakeDeps, CMakeToolchain if using CMake, and use the right commands to consume successfully your created package.
This is the part that could have the mistake, and all the details for this part are missing.

@zxuan99
Copy link
Author

zxuan99 commented Nov 15, 2024

Hi,

But it is a pre-built binary. Do I still have to use "generators"?

And another question is that I am using Visual Studio to link the libary. So am I supposed to use "MSBuild" or "CMake"?

@zxuan99
Copy link
Author

zxuan99 commented Nov 15, 2024

To add on, I am following the instructions stated in https://docs.conan.io/2/tutorial/creating_packages/other_types_of_packages/package_prebuilt_binaries.html - Packaging already Pre-built Binaries

It did not mention anything about CMake/MSBuild. May I ask if I am referring to the correct set of instructions? The library that I want to wish to be uploaded to Conan and used in Visual Studio is pre-built as mentioned.

@memsharded
Copy link
Member

memsharded commented Nov 15, 2024

But it is a pre-built binary. Do I still have to use "generators"?

In the consumer side, yes. It doesn't matter if the package is built from a pre-built binary or you build the package from source. When you are consuming the package, it is exactly the same, it is a binary.

And the consuming project will have its own build system to built itself, CMake, Meson, MSBuild, or whatever. The generators should be used in the consumer side, not necessary in the package side.

For example, when you use a conan new cmake_lib template, that contains 2 conanfile.py:

  • the "package" conanfile.py, that one is the one actually creating a package in the Conan cache, a package that can be uploaded
  • the test_package/conanfile.py, this one is a "consumer" conanfile, it doesn't create a package at all (note it doesn't have name or version. This test_package/conanfile.py will have generators to be able to consume correctly the created package.

When you write conan create . over this project, this happens:

  • First, the package is exported to the cache and created there, from the root conanfile.py
  • Then the test_package/conanfile.py consumer project is triggered, to use the just created package and check it was packaged correctly

@zxuan99
Copy link
Author

zxuan99 commented Nov 18, 2024

Hi,

This is my consumer conanfile.py

from conan import ConanFile
from conan.tools.microsoft import vs_layout, MSBuildDeps
class ConanApplication(ConanFile):
package_type = "application"
settings = "os", "compiler", "build_type", "arch"

def layout(self):
    vs_layout(self)

def generate(self):
    deps = MSBuildDeps(self)
    deps.generate()

def requirements(self):
    requirements = self.conan_data.get('requirements', [])
    for requirement in requirements:
        self.requires(requirement)

My producer conanfile.py is in the previous comment.

I am still facing the same issue - runtime error which shows an error that "uhd.dll" was not found

May I ask if there is any more lines that I need to add to my consumer conanfile.py so that it is able to find the "uhd.dll"?

@memsharded
Copy link
Member

The "consumer" conanfile, when doing conan install, will generate some .props files, like conan_uhd.props and conandeps.props files. See https://docs.conan.io/2/reference/tools/microsoft/msbuilddeps.html#msbuilddeps for more details.

Are you adding some of those files to your VS project? Read the paragraph from that page:

Add the conandeps.props to your solution project files if you want to depend on all the declared dependencies. For single project solutions, this is probably the way to go. For multi-project solutions, you might be more efficient and add properties files per project. You could add conan_zlib.props properties to “project1” in the solution and conan_bzip2.props to “project2” in the solution for example.

@zxuan99
Copy link
Author

zxuan99 commented Nov 18, 2024

I have checked the conan folder. It has both conandeps.props and conan_uhdp.props.

Within conandeps.props, it also has the following line under ImportGroup.
Import Condition="'$(conan_uhd_props_imported)' != 'True'" Project="conan_uhd.props"

@memsharded
Copy link
Member

You have to pick one of those files, depending on what you want, and add it to your VS project (like in the project view -> Property Manager view -> right button -> Add Existing Property View). Or edit directly your .vcxproj file if you know MSBuild syntax and add it there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants