0

I want to create dll file and link it. I refer this source : https://zh.wikibooks.org/wiki/CMake_%E5%85%A5%E9%96%80/%E5%BB%BA%E7%BD%AE%E8%88%87%E9%80%A3%E7%B5%90%E7%A8%8B%E5%BC%8F%E5%BA%AB

I do some modify for create dll and link it :

1. (./src/calc/CMakeLists.txt)

cmake_minimum_required(VERSION 2.6)

project(calc)
add_library(calc calc.c)

after modify =>

cmake_minimum_required(VERSION 2.6)

project(calc)
add_library(calc SHARED calc.c)

2. (./src/calc/calc.h)

#ifndef CALC_H_
#define CALC_H_

int Cube(int x);

int Square(int x);

#endif

after modify =>

#ifndef CALC_H_
#define CALC_H_ ()

#if defined(_WIN64) && defined(calc_EXPORTS)
#  if defined(LIBCALC_INTERNAL)
#    define LIBCALC_API __declspec (dllexport)
#  else
#    define LIBCALC_API __declspec (dllimport)
#  endif
#endif

#if !defined(LIBCALC_API)
#  define LIBCALC_API
#endif

LIBCALC_API int Cube(int x);

LIBCALC_API int Square(int x);

#endif
  1. (./src/calc/calc.c)

    int Cube(int x) { return x * x * x; }

    int Square(int x) { return x * x; }

after modify =>

#define LIBCALC_INTERNAL
#include "calc.h"

LIBCALC_API int Cube(int x)
{
    return x * x * x;
}

LIBCALC_API int Square(int x)
{
    return x * x;
}

Do cmake :

cmake ../src -G "Visual Studio 15 2017"

MSBuild test_proj.sln

Go to executable file directory to execute:

/build/app/app.exe

Result is "cannot find calc.dll"

I copy it form dll builed directory. It execute normally.

I want to execute after build. Don't copy dll form dll directory to executable file directory. Does anybody can help me?

D.Wei
  • 55
  • 9
  • You can write a function in your CMakeList.txt to copy the dll. Or just make the same bin folder for both projects. – drescherjm Sep 23 '18 at 14:49
  • Here is how you set the bin folder: https://stackoverflow.com/questions/6594796/how-do-i-make-cmake-output-into-a-bin-dir – drescherjm Sep 23 '18 at 14:51
  • Also note you can use `cmake --build . --config RelWithDebInfo` to build the solution – Botje Sep 23 '18 at 19:37
  • I am just very curious about dependency of most of open source, such as opencv. For instance, a.dll depend on b.dll, b.dll depend on c.dll. The sequency of build should be c -> b -> a. Why I can't encounter this problem when I build opencv with cmake? – D.Wei Sep 24 '18 at 02:50
  • I want to a function like "LIBS += /path/to/dir" of qmake that executable file can auto link to directory of " /path/to/dir". Don't copy dll after build. – D.Wei Sep 24 '18 at 02:54

0 Answers0