0

I have a CMake project that works fine on Linux with g++.

On Windows (10, Creators Update) CMake runs fine and generates relevant files.

So I opened TestProject.sln solution file and for a test run, I ran 'Local Windows Debugger' and got this error.

Error   LNK1104 cannot open file 'jsoncpp.lib'

This is how I've setup this project.

.
|-root
|  |- src
|  |- lib
|  |- bin
|  |- app
|  |- include
|  |- extras
|        |- jsoncpp
|-build

I'm not really sure what could be wrong here.

I have jsoncpp added as a submodule.

I did

git submodule update --init --recursive

and there is a jsoncpp.lib file in

root\lib\Debug

This is my CMakeLists.txt

cmake_minimum_required (VERSION 3.13)
project (TestProject)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/root/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/root/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/root/bin)

set(SOURCE_DIR ${PROJECT_SOURCE_DIR}/root/src)
set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/root/include)
set(EXTRAS_DIR ${PROJECT_SOURCE_DIR}/root/extras)
set(PROJECT_MAIN_DIR ${PROJECT_SOURCE_DIR}/root/app)

set(SOURCES
  ${INCLUDE_DIR}/Combinations.hpp  
  ${INCLUDE_DIR}/Lib.hpp  
  ${SOURCE_DIR}/Lib.cpp
  )

add_executable(TestProject ${PROJECT_MAIN_DIR}/Main.cpp ${SOURCES})

add_subdirectory(${EXTRAS_DIR}/jsoncpp jsoncpp)

target_link_libraries(TestProject jsoncpp)

set_target_properties(TestProject PROPERTIES
  CXX_STANDARD_REQUIRED ON
  CXX_STANDARD 17
  CXX_EXTENSIONS OFF
  )

This is CMake's output

Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.17134.
The C compiler identification is MSVC 19.16.27026.1
The CXX compiler identification is MSVC 19.16.27026.1
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
JsonCpp Version: 1.8.4
Looking for C++ include clocale
Looking for C++ include clocale - found
Looking for localeconv
Looking for localeconv - found
Looking for C++ include sys/types.h
Looking for C++ include sys/types.h - found
Looking for C++ include stdint.h
Looking for C++ include stdint.h - found
Looking for C++ include stddef.h
Looking for C++ include stddef.h - found
Check size of lconv
Check size of lconv - done
Performing Test HAVE_DECIMAL_POINT
Performing Test HAVE_DECIMAL_POINT - Success
Found PythonInterp: C:/Users/USERNAME/emacs-26.1-x86_64/bin/python2.exe (found suitable version "2.7.14", minimum required is "2.6") 
Configuring done
Generating done

I'm not very familiar with Visual Studio but looking at the error, it says 'cannot open' and not 'couldn't find' so my first thought was maybe it's a permission issue.

So I booted up VS with Admin Privileges but the error continued.

I thought maybe there's some compiler issue so I used nmake.exe as a compiler (after generating nmake makefiles) but no luck there either.

I'm not sure if it's a linker issue since CMake was able to find and link jsoncpp library so I doubt CMake made a mistake building the Makefile.

I'm not sure how I should go about debugging the issue.

iovo
  • 99
  • 1
  • 6
  • 1
    It seems you need to specify library location in [link_directories](https://cmake.org/cmake/help/v3.13/command/link_directories.html). On Linux you may have jsoncpp installed system-wide and that global library is linked. – dewaffled Jan 31 '19 at 15:31
  • ***first thought was maybe it's a permission issue*** No it is a location problem. You need to tell it where to look for your library. It won't look in `CMAKE_LIBRARY_OUTPUT_DIRECTORY` by default. – drescherjm Jan 31 '19 at 15:35
  • @dewaffled I avoided using link_directories because as mentioned in this [link](https://stackoverflow.com/questions/31438916/cmake-cannot-find-library-using-link-directories). Should I use something else or go with link_directories? – iovo Jan 31 '19 at 15:37
  • @dewaffled This is what I added to my CMakeLists.txt file `link_directories(${EXTRAS_DIR}/jsoncpp)`. But the error persists. I'm I missing something? – iovo Jan 31 '19 at 15:43
  • Pass absolute path to jsoncpp library starting with `${CMAKE_SOURCE_DIR}`. – arrowd Jan 31 '19 at 15:44
  • @arrowd `link_directories(${CMAKE_SOURCE_DIR}/extras/jsoncpp)` where I made sure `CMAKE_SOURCE_DIR` is pointing to right dir (removed build dir and tried in source building by moving CMakeLists into root, changing paths). Error still persists tho :( – iovo Jan 31 '19 at 16:22
  • I probably should be `link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY})` I highly doubt that the generated `.lib` file is in `${CMAKE_SOURCE_DIR}/extras/jsoncpp` – drescherjm Jan 31 '19 at 16:46
  • @drescherjm That unfortunately didn't work. AFAIK `${CMAKE_LIBRARY_OUTPUT_DIRECTORY}` referring to an output dir without a jsoncpp.lib file. Although there's an empty Debug folder which I think should have jsoncpp.lib file. – iovo Jan 31 '19 at 18:50
  • ***Although there's an empty Debug folder which I think should have `jsoncpp.lib` file*** You will need to fix that first. Not sure how there is currently not enough information to help. – drescherjm Jan 31 '19 at 19:08
  • @drescherjm That was just a guess but if that's true, I think that could be the problem. There's an empty `Debug` folder in `build` dir but there's also another folder `root/lib/Debug` and there **are** `jsoncpp.exp` and `jsoncpp.lib` files. Most likely placed there by these commands `set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/root/lib)` from my CMake file. Removing them doesn't help tho – iovo Jan 31 '19 at 19:15
  • 3
    What sources are you using for jsoncpp? The version I saw on github doesn't even create a `jsoncpp` target. The example projects to test the library use the command: `target_link_libraries(jsoncpp_test jsoncpp_lib)`. So unless the recipe creates a target called `jsoncpp` to link against you are trying to use some random library in the default search paths. – fdk1342 Jan 31 '19 at 19:23
  • Where is `root/lib/Debug` versus `${PROJECT_SOURCE_DIR}/root/lib`? – drescherjm Jan 31 '19 at 19:27
  • Like @Fred said if the `CMakeLists.txt` for `jsoncpp` that you are using has a target named `jsoncpp_lib` using that in the target_link_libraries() should work instead of the library name. I did not follow this path because I don't know what you have in that link so I was trying to solve the linker directory problem. – drescherjm Jan 31 '19 at 19:29
  • One other thing if this library has an exp file the library is most likely a shared library . You may have to adjust the paths or copy the dll to run your code. – drescherjm Jan 31 '19 at 19:32
  • Yes! That was the cause! I missed `_lib` part when debugging the file a couple of days ago. Apologies for taking your time, I should've been more careful. Thanks for the help tho! – iovo Jan 31 '19 at 19:38
  • @drescherjm Thanks for the advice, I'll look into it. For now the project compiled just fine and running smoothly – iovo Jan 31 '19 at 19:45

0 Answers0