0

I am trying to create dynamic library in windows. I followed the instruction in dart-ffi.

This is my CMakelists.txt

cmake_minimum_required(VERSION 3.20.2 FATAL_ERROR)
project(libgit VERSION 1.0.0 LANGUAGES C)
add_library(libgit SHARED git2.h)

set_target_properties(libgit PROPERTIES
    PUBLIC_HEADER git2dart.h
    VERSION {PROJECT_VERSION}
    SOVERSION 1
    LINKER_LANGUAGE C
    OUTPUT_NAME "git2dart"
)

You can clone the repo using in gh repo clone nivekithan/git2dart

You will find the CMakeFiles.txt in the directory libgit2.

If I were run cmake . from this directory then I would get this message

 Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.21376.
-- The C compiler identification is MSVC 19.28.29914.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/nivek/Documents/work/git2dart/libgit2

But then If I were to run make I would get this message

make: *** No targets specified and no makefile found.  Stop.

Thanks for the help

Nivekithan
  • 953
  • 1
  • 6
  • 10
  • You forgot to add source files to your library. Header files are not compiled and therefore no `SHARED` library target is created. You cannot create a shared library from headers only. – vre May 12 '21 at 14:58
  • @vre Thanks for the reply, Basically I dont know anything about `c` language and its ecosystem I am trying to create `dart` bindings for [libgit2](https://github.com/libgit2/libgit2) . To achieve that I just copy pasted `inlcude` directory in from `libgit2` to my package. Which only contains `.h` files, it does not contain `.c` files. So if you have time can you please guide me on what I am supposed to do to make it work – Nivekithan May 12 '21 at 15:06
  • What build system does `dart` use? You probably don't need CMake at all. – arrowd May 12 '21 at 15:14
  • @arrowd from this [blog](https://medium.com/@john.p.ryan4/exploring-darts-new-build-system-689ded1183d8#:~:text=If%20you've%20ever%20worked,new%20system%20called%20package%3Abuild%20.) it seems dart uses build system called `package:build`. And I am pretty sure that we have to use `cmake` to `make` since even in [offical documentation](https://dart.dev/guides/libraries/c-interop) they are using `cmake` and `make` – Nivekithan May 12 '21 at 15:22
  • 1
    The first line in your output - "Building for: Visual Studio 16 2019" - means that project is configured for **Visual Studio**, so it should be built using Visual Studio, not with the `make` utility. For build using `make`, you need to tell CMake about your intention. This is performed by setting appropriate [generator](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html) using `-G` option. E.g. on Windows you could use `NMake Makefiles` generator. – Tsyvarev May 12 '21 at 15:33
  • Note that you can build a Visual Studio project via cmake; You need to consider 2 things though: The default architecture is 32 bit; you can change this by passing the architecture during generation(`cmake -A x64 ...`). Also of course you cannot use make specific commands; personally I prefer using cmake to build the project from command line anyways, since the command line interface works for all generators not just make; note that the only thing you wouldn't pass when using make is the `--config ...` part: `cmake --build . --config Release` – fabian May 12 '21 at 16:34
  • @fabian thanks for the reply. At first i tried `cmake -A x64` which works fine. But I am not able to understand `make` part. Can you please write it simpler, I have no prior experience with c ecosystem – Nivekithan May 12 '21 at 16:47
  • 1
    With cmake you can set up projects for different build systems. E.g. at work I use Visual Studio 2019 on Windows (with the corresponding generator) and on Linux make+GCC. Make only provides a single configuration, so the `--config Release` part to select the release configuration does not work on linux. Basically it's `cmake -D CMAKE_BUILD_TYPE=Release -S source -B build && cmake --build build` and `cmake -A x64 -S src -B build` `cmake --build build --config Release` to setup the project in `src` in directory `build` and build the Release configuration on linux / win respectively – fabian May 12 '21 at 17:01
  • @fabian thank you for reply and your patience. it works, Once the command finished executing the commands it creates a build directory and everything seems okay except I still cant find a single `dll` file. I only thing that seems close to `dll` file is `build/libgit.dir/Release/git2dart.dll.recipe` and I dont think they are same or do they? – Nivekithan May 12 '21 at 17:15
  • @fabian it could be due to how I have only `.h` file not `.c` file. I just copied `include` directory from this [libgit2](https://github.com/libgit2/libgit2). If thats the problem, then I think I should copy `src` directory from `libgit2` repo. Then what I should specify in `add_library(libgit shared ??>)` – Nivekithan May 12 '21 at 17:20
  • Note that cmake does set up a Visual Studio solution. You can use `cmake --open build` for build directory `build` to open this solution which allows you to inspect the properties of the target; this should contain the info about the location where the dll is placed, but usually it's in a subdirectory of the build directory that has a name matching the configuration, but searching the build directory after a successful build for anything ending with `.dll` should also allow you to locate the dll. – fabian May 12 '21 at 17:34
  • I'm not sure why you're having to create any code. As libgit2 is C, you should be able to use ffigen to generate the Dart code to talk to the libgit DLL (which you will build normally). – Richard Heap May 21 '21 at 11:12

1 Answers1

0

cmake /? and you will see the list of the available generators. At the moment you generate for Visual Studio not not make. I personally prefer ninja

Usage

  cmake [options] <path-to-source>
  cmake [options] <path-to-existing-build>
  cmake [options] -S <path-to-source> -B <path-to-build>

Specify a source directory to (re-)generate a build system for it in the
current working directory.  Specify an existing build directory to
re-generate its build system.

Options
  -S <path-to-source>          = Explicitly specify a source directory.
  -B <path-to-build>           = Explicitly specify a build directory.
  -C <initial-cache>           = Pre-load a script to populate the cache.
  -D <var>[:<type>]=<value>    = Create or update a cmake cache entry.
  -U <globbing_expr>           = Remove matching entries from CMake cache.
  -G <generator-name>          = Specify a build system generator.
  -T <toolset-name>            = Specify toolset name if supported by
                                 generator.
  -A <platform-name>           = Specify platform name if supported by
                                 generator.
  -Wdev                        = Enable developer warnings.
  -Wno-dev                     = Suppress developer warnings.
  -Werror=dev                  = Make developer warnings errors.
  -Wno-error=dev               = Make developer warnings not errors.
  -Wdeprecated                 = Enable deprecation warnings.
  -Wno-deprecated              = Suppress deprecation warnings.
  -Werror=deprecated           = Make deprecated macro and function warnings
                                 errors.
  -Wno-error=deprecated        = Make deprecated macro and function warnings
                                 not errors.
  -E                           = CMake command mode.
  -L[A][H]                     = List non-advanced cached variables.
  --build <dir>                = Build a CMake-generated project binary tree.
  --install <dir>              = Install a CMake-generated project binary
                                 tree.
  --open <dir>                 = Open generated project in the associated
                                 application.
  -N                           = View mode only.
  -P <file>                    = Process script mode.
  --find-package               = Run in pkg-config like mode.
  --graphviz=[file]            = Generate graphviz of dependencies, see
                                 CMakeGraphVizOptions.cmake for more.
  --system-information [file]  = Dump information about this system.
  --log-level=<ERROR|WARNING|NOTICE|STATUS|VERBOSE|DEBUG|TRACE>
                               = Set the verbosity of messages from CMake
                                 files.  --loglevel is also accepted for
                                 backward compatibility reasons.
  --log-context                = Prepend log messages with context, if given
  --debug-trycompile           = Do not delete the try_compile build tree.
                                 Only useful on one try_compile at a time.
  --debug-output               = Put cmake in a debug mode.
  --debug-find                 = Put cmake find in a debug mode.
  --trace                      = Put cmake in trace mode.
  --trace-expand               = Put cmake in trace mode with variable
                                 expansion.
  --trace-format=<human|json-v1>
                               = Set the output format of the trace.
  --trace-source=<file>        = Trace only this CMake file/module.  Multiple
                                 options allowed.
  --trace-redirect=<file>      = Redirect trace output to a file instead of
                                 stderr.
  --warn-uninitialized         = Warn about uninitialized values.
  --warn-unused-vars           = Warn about unused variables.
  --no-warn-unused-cli         = Don't warn about command line options.
  --check-system-vars          = Find problems with variable usage in system
                                 files.
  --profiling-format=<fmt>     = Output data for profiling CMake scripts.
                                 Supported formats: google-trace
  --profiling-output=<file>    = Select an output path for the profiling data
                                 enabled through --profiling-format.
  --help,-help,-usage,-h,-H,/? = Print usage information and exit.
  --version,-version,/V [<f>]  = Print version number and exit.
  --help-full [<f>]            = Print all help manuals and exit.
  --help-manual <man> [<f>]    = Print one help manual and exit.
  --help-manual-list [<f>]     = List help manuals available and exit.
  --help-command <cmd> [<f>]   = Print help for one command and exit.
  --help-command-list [<f>]    = List commands with help available and exit.
  --help-commands [<f>]        = Print cmake-commands manual and exit.
  --help-module <mod> [<f>]    = Print help for one module and exit.
  --help-module-list [<f>]     = List modules with help available and exit.
  --help-modules [<f>]         = Print cmake-modules manual and exit.
  --help-policy <cmp> [<f>]    = Print help for one policy and exit.
  --help-policy-list [<f>]     = List policies with help available and exit.
  --help-policies [<f>]        = Print cmake-policies manual and exit.
  --help-property <prop> [<f>] = Print help for one property and exit.
  --help-property-list [<f>]   = List properties with help available and
                                 exit.
  --help-properties [<f>]      = Print cmake-properties manual and exit.
  --help-variable var [<f>]    = Print help for one variable and exit.
  --help-variable-list [<f>]   = List variables with help available and exit.
  --help-variables [<f>]       = Print cmake-variables manual and exit.

Generators

The following generators are available on this platform (* marks default):
* Visual Studio 16 2019        = Generates Visual Studio 2019 project files.
                                 Use -A option to specify architecture.
  Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files.
                                 Optional [arch] can be "Win64" or "IA64".
  Visual Studio 9 2008 [arch]  = Generates Visual Studio 2008 project files.
                                 Optional [arch] can be "Win64" or "IA64".
  Borland Makefiles            = Generates Borland makefiles.
  NMake Makefiles              = Generates NMake makefiles.
  NMake Makefiles JOM          = Generates JOM makefiles.
  MSYS Makefiles               = Generates MSYS makefiles.
  MinGW Makefiles              = Generates a make file for use with
                                 mingw32-make.
  Unix Makefiles               = Generates standard UNIX makefiles.
  Green Hills MULTI            = Generates Green Hills MULTI files
                                 (experimental, work-in-progress).
  Ninja                        = Generates build.ninja files.
  Ninja Multi-Config           = Generates build-<Config>.ninja files.
  Watcom WMake                 = Generates Watcom WMake makefiles.
  CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
  CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
  CodeBlocks - NMake Makefiles JOM
                               = Generates CodeBlocks project files.
  CodeBlocks - Ninja           = Generates CodeBlocks project files.
  CodeBlocks - Unix Makefiles  = Generates CodeBlocks project files.
  CodeLite - MinGW Makefiles   = Generates CodeLite project files.
  CodeLite - NMake Makefiles   = Generates CodeLite project files.
  CodeLite - Ninja             = Generates CodeLite project files.
  CodeLite - Unix Makefiles    = Generates CodeLite project files.
  Sublime Text 2 - MinGW Makefiles
                               = Generates Sublime Text 2 project files.
  Sublime Text 2 - NMake Makefiles
                               = Generates Sublime Text 2 project files.
  Sublime Text 2 - Ninja       = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles
                               = Generates Sublime Text 2 project files.
  Kate - MinGW Makefiles       = Generates Kate project files.
  Kate - NMake Makefiles       = Generates Kate project files.
  Kate - Ninja                 = Generates Kate project files.
  Kate - Unix Makefiles        = Generates Kate project files.
  Eclipse CDT4 - NMake Makefiles
                               = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - MinGW Makefiles
                               = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Ninja         = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Thanks for the reply, I was able to understand from your answers and others that I am generating for visual studio. So can you please tell me what should be my next step in creating the `.dll` file. By the way I installed visual studio just so that I can install c++ development tools , I dont use it as `IDE` – Nivekithan May 12 '21 at 16:50