2

I am trying to convert a Visual Studio 2013 project that I have over to use CMake, but am running into issues when I include a source header file that uses some shared libraries. In my original VS project properties under Common Properties > References I have added several references I need in my project. There are options like a relative path to the library, some build settings and and so on. The XML generated in my VS project file looks very similar to the following:

<Project>
.
.
.
  <ItemGroup>
    <Reference Include="MyLib">
      <HintPath>../lib/MyLib.dll</HintPath>
    </Reference>
    <Reference Include="System">
      <CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies>
      <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
    </Reference>
    .
    .
    .
  </ItemGroup>

Basically I want to be able to use 'MyLib.dll', 'System', and my other references, but I don't know how to include them via CMake.

I did some Googling and found this: CMake link to external library, but that didn't seem to work. Any ideas what commands I need to use to use the shared libraries?

EDIT:

A minimal version of my current CMakeLists.txt file

cmake_minimum_required(VERSION 3.0.2)
project(MyProject)
set(EXE_NAME "MyExe")

set(SOURCES
    "src/main.cpp"
    "src/SourceUsingDLL.cpp"
    "src/others.cpp"
)

set(HEADERS
    "include/HeaderUsingDLL.h"
    "include/otherHeaders.h"
)

include_directories(include)

add_executable(${EXE_NAME} ${SOURCES} ${HEADERS})

### What I tried - Didn't work ####
add_library( mylib SHARED IMPORTED )
set_target_properties( mylib PROPERTIES IMPORTED_LOCATION pathToDLL )
target_link_libraries(${EXE_NAME} mylib)

For the dll path I used both relative and absolute for my local dll, but I don't what what I would do for the System reference.

Community
  • 1
  • 1
MrJman006
  • 752
  • 10
  • 26
  • 2
    can you post a (very) minimal (working) example of your cmake file so we can see what you tried? – Richard Hodges Jan 20 '16 at 22:10
  • Seems like what you're trying to achieve would be similar to basically setting up the `PATH` environment variable so the executable can actually find the dll at runtime. If that is correct, I don't think that CMake actually has built-in support for that (which is very unfortunate). The framework that I'm working with uses CMake to generate a `bat` file which sets up the environment either before the IDE is run or before the executable is run. – Rostislav Jan 20 '16 at 23:46
  • @RichardHodges I added a minimal version of my current CMakeLists.txt – MrJman006 Jan 21 '16 at 14:47
  • @Rostislav So you think this might just be a feature of Visual Studio and not possible in CMake? Is there no way to setup a path for the build to use. I mean when I change the location of my DLLS with the build in VS, my program won't run, so it is just telling it where to look. – MrJman006 Jan 21 '16 at 14:50
  • there's no call to `target_link_libraries(${EXE_NAME} mylib)` – Richard Hodges Jan 21 '16 at 14:53
  • Sorry about that I actually had the line in my cmake file, just forgot to copy it over. – MrJman006 Jan 21 '16 at 15:26
  • @MrJman006 The build step doesn't use the `dll`, it uses the import `lib` (just to make sure we're on the same page). This problem is not completely specific to MS toolchain though - the shared libraries in *nix systems can have similar problems. In Linux it's a smaller issue though due to the RPATH mechanism that is embedded in the executable, but sometimes it has similar issues. And I'm afraid that your best bet would be either the `install` target which will copy the exe and dlls to same location, or bat file generation. I would love to be corrected though - I'm somewhat suffering myself :) – Rostislav Jan 21 '16 at 18:24
  • @Rostislav Yes I understand what you mean. I may have said it wrong, but I understand the dll is not built into my executable. Now correct me if I am wrong, but does Windows not have a mechanism similar to RPATH? One link came across talked about using CMake to setup RPATH and to make sure the library is installed at one of the paths in RPATH? – MrJman006 Jan 21 '16 at 19:42
  • Does your library have `.lib` file? – arrowd Jan 22 '16 at 15:36
  • @MrJman006 I'm not aware of a similar mechanism on windows - this is what MSDN has to say on the dll search order: [link](https://msdn.microsoft.com/en-us/library/7d83bc18.aspx). – Rostislav Jan 26 '16 at 00:01

0 Answers0