3

I have been using nana library for a while for my application on Windows and it works great. Now I am trying to do a Linux build but I can not seem to link nana to my application correctly.

I have tried this but it also did not seem to work

I have created a small example to demonstrate the issue. Here is my main.cpp

#include <nana/gui/filebox.hpp>
#include <iostream>

int main()
{
    nana::filebox picker{nullptr, true};

    auto paths = picker.show();
    if(paths.empty())
    {
        std::cout<<"Cancelled"<<std::endl;
    }
    else
    {
        for(auto & p : paths)
            std::cout << "Selected file:" << p << std::endl;
    }

}

and my CMakeList looks like this:

cmake_minimum_required(VERSION 3.0.0)
project(NanaTest VERSION 0.1.0)

include(CTest)
enable_testing()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall")
#set(CMAKE_LINK_WHAT_YOU_USE TRUE)

add_executable(NanaTest main.cpp)

target_include_directories(NanaTest PRIVATE
    usr/local/include/nana)

find_library(NANALIB NAMES libnana.a  REQUIRED PATHS usr/local/lib/)

if(NOT NANALIB)
    message([FATAL_ERROR] "NANALIB not found")
endif()

target_link_libraries(NanaTest ${NANALIB})
#target_link_libraries(${PROJECT_NAME} -lnana)
target_link_libraries(${PROJECT_NAME} -lX11)
target_link_libraries(${PROJECT_NAME} -lXcursor)
target_link_libraries(${PROJECT_NAME} -lpthread )
target_link_libraries(${PROJECT_NAME} -lXft )
target_link_libraries(${PROJECT_NAME} -lfontconfig )

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

This seems to work when I run. However, it does not work when I copy it to another VM. Also the ldd result does not seem to contain libnana.so the shared object. When I run this on the other VM I get segmentation fault and when I run it with gdb this is the error I am getting:

Program received signal SIGSEGV, Segmentation fault.
0x0000555555b80a21 in nana::detail::platform_spec::platform_spec() ()

When I uncomment set(CMAKE_LINK_WHAT_YOU_USE TRUE) and target_link_libraries(${PROJECT_NAME} -lnana)

I can see the libnana.so in ldd result however this time when application ends I get a double free or corruption (!prev) Aborted (core dumped) error. Does anybody know what the issue could be. I am using GCC 9.3.0. Thank you in advance. By the way I am using nana-hotfix-1.7.4 but I tried 1.7.1, 1.7.2 and 1.7.3 they have the same issue.

Ali Kanat
  • 1,888
  • 3
  • 13
  • 23
  • 1
    "However, it does not work when I copy it to another VM." - Probably, some libraries (e.g. X11) on another VM are incompatible with ones for which your application is compiled. "Also the ldd result does not seem to contain libnana.so the shared object." - You link with `libnana.a` (a **static** library). Why do you expect `libnana.so` (**shared** library) to be appeared in `ldd` results? – Tsyvarev Aug 17 '21 at 08:46
  • To be honest. I am a bit dense about the topic regarding linking. I have used other 3rd parties and they required shared libraries but they are probably secondary dependencies of these static linked 3rd party libraries. Let me also copy shared libraries to the VM and try modifying LD_LIBRARY_PATH to exe folder. – Ali Kanat Aug 17 '21 at 08:57
  • @Tsyvarev I copied every library to the build folder and modified the LD_LIBRARY_PATH accordingly but i am still getting the Segmentation fault. I think it is not related to X11 or another library. – Ali Kanat Aug 17 '21 at 09:22

1 Answers1

3

Here's a build that actually works:

cmake_minimum_required(VERSION 3.21)
project(NanaTest VERSION 0.1.0)

include(FetchContent)
FetchContent_Declare(
  nana
  GIT_REPOSITORY https://github.com/cnjinhao/nana.git
  GIT_TAG        v1.7.4
  GIT_SHALLOW    TRUE
)

FetchContent_MakeAvailable(nana)

add_executable(NanaTest main.cpp)
target_link_libraries(NanaTest PRIVATE nana)

The nana developers really, REALLY want you to include their sources in your build. The easiest way to do that is via FetchContent.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • I have seen that warning but I could not make much sense out of it. It works now. Thank you so much. I have been trying to fix this for days. – Ali Kanat Aug 18 '21 at 08:29