# miniupnpc libdir not detected properly



## jphovila (Sep 27, 2022)

Attempting to port the GNTL daemon and wallet to FreeBSD 13.1.

There's one show stopper I haven't been able to solve yet, and would appreciate any ideas people here may have.

First, installed dependencies (adapter for FreeBSD based on GNTLs README.md, the list may grow or change later)

`pkg install git autoconf automake boost-all cmake gmake doxygen expat gcc gperf graphviz googletest hidapi miniupnpc ldns libevent lzmalib readline libsodium libtool libgudev unbound libunwind libzmq4 pkgconf libxslt`

Then, created a dedicated user (gntld) and (as that user) did the following (based on https://github.com/The-GNTL-Project/gntl/):


```
[gntld@gntl ~]$ git clone --recursive [URL]https://github.com/The-GNTL-Project/gntl[/URL]

[gntld@gntl ~]$ cd gntl && git checkout v1.0.4

[gntld@gntl ~/gntl]$ USE_SINGLE_BUILDDIR=1 gmake release
mkdir -p build/release
cd build/release && cmake -D CMAKE_BUILD_TYPE=Release ../..
-- CMake version 3.23.2
-- Building without build tag
-- Checking submodules
-- Submodule 'external/miniupnp' is up-to-date
-- Submodule 'external/unbound' is up-to-date
-- Submodule 'external/rapidjson' is up-to-date
-- Submodule 'external/randomarq' is up-to-date
-- Building for a 64-bit system
-- Using LMDB as default DB type
-- looking for liblzma
-- liblzma found
-- Stack trace on exception enabled (using libunwind)
-- Using OpenSSL include dir at /usr/include
-- Found miniupnpc
CMake Error at CMakeLists.txt:108 (target_link_libraries):
  Cannot specify link libraries for target "miniupnpc" which is not built by
  this project.
Call Stack (most recent call first):
  external/CMakeLists.txt:48 (link_dep_libs)

-- Configuring incomplete, errors occurred!
See also "/home/gntld/gntl/build/release/CMakeFiles/CMakeOutput.log".
See also "/home/gntld/gntl/build/release/CMakeFiles/CMakeError.log".
gmake: *** [Makefile:85: cmake-release] Error 1
```

At the end of CMakeError.log is this:

```
Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
Change Dir: /home/gntld/gntl/build/release/CMakeFiles/CMakeTmp

Run Build Command(s):/usr/local/bin/gmake -f Makefile cmTC_35c1e/fast && gmake[1]: Entering directory '/usr/home/gntld/gntl/build/release/CMakeFiles/CMakeTmp'
/usr/local/bin/gmake  -f CMakeFiles/cmTC_35c1e.dir/build.make CMakeFiles/cmTC_35c1e.dir/build
gmake[2]: Entering directory '/usr/home/gntld/gntl/build/release/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_35c1e.dir/src.c.o
/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD   -MD -MT CMakeFiles/cmTC_35c1e.dir/src.c.o -MF CMakeFiles/cmTC_35c1e.dir/src.c.o.d -o CMakeFiles/cmTC_35c1e.dir/src.c.o -c /home/gntld/gntl/build/release/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_35c1e
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_35c1e.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/cmTC_35c1e.dir/src.c.o -o cmTC_35c1e
ld: error: undefined symbol: pthread_create
>>> referenced by src.c
>>>               CMakeFiles/cmTC_35c1e.dir/src.c.o:(main)
cc: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[2]: *** [CMakeFiles/cmTC_35c1e.dir/build.make:100: cmTC_35c1e] Error 1
gmake[2]: Leaving directory '/usr/home/gntld/gntl/build/release/CMakeFiles/CMakeTmp'
gmake[1]: *** [Makefile:127: cmTC_35c1e/fast] Error 2
gmake[1]: Leaving directory '/usr/home/gntld/gntl/build/release/CMakeFiles/CMakeTmp'
```

Source file was:

```
#include <pthread.h>

static void* test_func(void* data)
{
  return data;
}

int main(void)
{
  pthread_t thread;
  pthread_create(&thread, NULL, test_func, NULL);
  pthread_detach(thread);
  pthread_cancel(thread);
  pthread_join(thread, NULL);
  pthread_atfork(NULL, NULL, NULL);
  pthread_exit(NULL);

  return 0;
}
```

The above may not be the real issue, since "The errors being written to CMakeErrors.log during cmake-time are **also** from the tests being run to determine what works and what doesn't. So there's no harm in there being errors in there" (https://www.mail-archive.com/kde-freebsd@kde.org/msg36289.html)


Returning to the original error message:

```
-- Found miniupnpc
CMake Error at CMakeLists.txt:108 (target_link_libraries):
  Cannot specify link libraries for target "miniupnpc" which is not built by
  this project.
Call Stack (most recent call first):
  external/CMakeLists.txt:48 (link_dep_libs)
```


Checking ./CMakeLists.txt, I find the following:

```
99 # Properly links a target to a list of library names by finding the given libraries.  Takes:
100 # - a target
101 # - a linktype (e.g. INTERFACE, PUBLIC, PRIVATE)
102 # - a library search path (or "" for defaults)
103 # - any number of library names
104 function(link_dep_libs target linktype libdirs)
105   foreach(lib ${ARGN})
106     find_library(link_lib-${lib} NAMES ${lib} PATHS ${libdirs})
107     if(link_lib-${lib})
108       target_link_libraries(${target} ${linktype} ${link_lib-${lib}})
109     endif()
110   endforeach()
111 endfunction()
```


And in external/CMakeLists.txt:

```
46 if(MINIUPNPC_FOUND)
 47   message(STATUS "Found miniupnpc")
 48   link_dep_libs(miniupnpc INTERFACE "${MINIUPNPC_LIBRARY_DIRS}" ${MINIUPNPC_LIBRARIES})
 49   target_include_directories(miniupnpc INTERFACE ${MINIUPNPC_INCLUDE_DIRS})
```


Doing a grep:

```
[gntld@gntl ~/gntl]$ grep -Er "MINIUPNPC_LIB|MINIUPNPC_INCLUDE" *
build/release/CMakeCache.txt:MINIUPNPC_INCLUDEDIR:INTERNAL=/usr/local/include
build/release/CMakeCache.txt:MINIUPNPC_INCLUDE_DIRS:INTERNAL=/usr/local/include
build/release/CMakeCache.txt:MINIUPNPC_LIBDIR:INTERNAL=/usr/local/
build/release/CMakeCache.txt:MINIUPNPC_LIBRARIES:INTERNAL=miniupnpc
build/release/CMakeCache.txt:MINIUPNPC_LIBRARY_DIRS:INTERNAL=/usr/local/
build/release/CMakeCache.txt:MINIUPNPC_LIBS:INTERNAL=
build/release/CMakeCache.txt:MINIUPNPC_LIBS_L:INTERNAL=
build/release/CMakeCache.txt:MINIUPNPC_LIBS_OTHER:INTERNAL=
build/release/CMakeCache.txt:MINIUPNPC_LIBS_PATHS:INTERNAL=
external/miniupnp/miniupnpc-libevent/miniupnpc-libevent.h:#ifndef MINIUPNPC_LIBEVENT_H_INCLUDED
external/miniupnp/miniupnpc-libevent/miniupnpc-libevent.h:#define MINIUPNPC_LIBEVENT_H_INCLUDED
external/miniupnp/miniupnpc-libevent/miniupnpc-libevent.h:#define MINIUPNPC_LIBEVENT_API_VERSION 1
external/miniupnp/miniupnpc-libevent/miniupnpc-libevent.h:#endif /* MINIUPNPC_LIBEVENT_H_INCLUDED */
external/CMakeLists.txt:  link_dep_libs(miniupnpc INTERFACE "${MINIUPNPC_LIBRARY_DIRS}" ${MINIUPNPC_LIBRARIES})
external/CMakeLists.txt:  target_include_directories(miniupnpc INTERFACE ${MINIUPNPC_INCLUDE_DIRS})
external/CMakeLists.txt:  set(MINIUPNPC_LIB "libminiupnpc-static" PARENT_SCOPE)
src/cryptonote_protocol/CMakeLists.txt:    ${MINIUPNPC_LIB}
src/p2p/CMakeLists.txt:    ${MINIUPNPC_LIB}
[code]

==> Should MINIUPNPC_LIBDIR and MINIUPNPC_LIBRARY_DIRS be /usr/local/lib (instead of /usr/local) ?
    - Probably, since looking at build/release/CMakeCache.txt I find:

[code]
...
UNBOUND_FOUND:INTERNAL=1
UNBOUND_LDFLAGS:INTERNAL=-L/usr/local/lib;-lunbound
UNBOUND_LIBDIR:INTERNAL=/usr/local/lib
UNBOUND_LIBRARY_DIRS:INTERNAL=/usr/local/lib
...
SODIUM_LDFLAGS:INTERNAL=-L/usr/local/lib;-lsodium
SODIUM_LIBDIR:INTERNAL=/usr/local/lib
SODIUM_LIBRARY_DIRS:INTERNAL=/usr/local/lib
```
    ... while for miniupnpc:

```
MINIUPNPC_LDFLAGS:INTERNAL=-L/usr/local/;-lminiupnpc
MINIUPNPC_LIBDIR:INTERNAL=/usr/local/
MINIUPNPC_LIBRARY_DIRS:INTERNAL=/usr/local/
```

What would be the proper way to fix this? 

Versions of the installed dependencies:

```
autoconf-2.71
automake-1.16.5
boost-all-1.79.0
cmake-3.23.2
gmake-4.3_2
doxygen-1.9.4,2
expat-2.4.8
gcc-11_4
gperf-3.1
graphviz-2.50.0_5
googletest-1.12.1
hidapi-0.12.0
miniupnpc-2.2.3_1
ldns-1.8.1
libevent-2.1.12
lzmalib-0.0.1_1
readline-8.1.2
libsodium-1.0.18
libtool-2.4.7
libgudev-234
unbound-1.16.2
libunwind-20211201_1
libzmq4-4.3.4
pkgconf-1.8.0,1
libxslt-1.1.35_3
```


----------



## covacat (Sep 27, 2022)

add -lthr


----------



## jphovila (Sep 27, 2022)

covacat said:


> add -lthr



Thanks! Although: where exactly should I add it?


----------



## W.hâ/t (Sep 27, 2022)

I suggest that you read this first.
https://docs.freebsd.org/en/books/porters-handbook/

I beleive I can fly. Oops...

I beleive you have to create the ports Makefile first. There are options you can set that helps the building process.


----------

