CMake project for JNI

2019-06-06 05:50发布

问题:

Ok so my intention is to create dynamic libraries for mac, windows and linux to be used with Java (both 32 and 64 bit).

I am using Jetbrains products for everything and in my mind I envisioned that, with TeamCity I could have build agents for each platform (which I have set up already) that would then produce the libraries for each platform.

My CMake project is made in CLion. I am a little bit confused by CMake though.

This is currently my cmakelists.txt:

cmake_minimum_required(VERSION 3.7)

project(TestProject)
set(CMAKE_CXX_STANDARD 14)

function(build bits)
    message("Building ${bits} bits...")
    set(CMAKE_BUILD_TYPE Release)

    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin${bits}")
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "bin${bits}")

    if (WIN32)
        include_directories("C:/Program Files/Java/jdk1.8.0_121/include" "C:/Program Files/Java/jdk1.8.0_121/include/win32")
    endif (WIN32)

    #Includes for linux and mac here

    set(SOURCE_FILES src/main.cpp)
    set(EXEC_SOURCE_FILES ${SOURCE_FILES} src/main.cpp)
    add_library(TestProject SHARED ${SOURCE_FILES})
endfunction()

if (CMAKE_SIZEOF_VOID_P EQUAL 8)
    build(64)
else (CMAKE_SIZEOF_VOID_P EQUAL 8)
    build(32)
endif (CMAKE_SIZEOF_VOID_P EQUAL 8)

I'm confused by the concept of CMake, so far, I thought it was kind of like a setup generator, and you then run it on the machine you want to set your progam up on, but I don't understand why then it is already creating my dll library.

That is the behavior I want though, I don't want a setup to run on a machine, I want my .dll and .so files prepped and ready to go and that's it.

Anyways, to get to the question, is the produced library actually usable for distribution? If so, how do I make it generate the .so file on linux, and the .dll file on windows with the same cmakelists.txt? Furthermore, do dependencies get added into the library?

Lastly, does it matter what compiler CMake will use? I have clang on mac, cygwin and mingw-64 on windows, and I think there's just gcc on ubuntu.

I might be too stupid or something, but I can't seem to understand CMake.

Summary list of my requirements:

  • Library needs to be built in 32 and 64 bits (for 32 and 64 bits Java) Compiling 32 and 64 bit
  • It needs to be built as .dll and .so
  • If there are dependencies, they should be put alongside the dll/so in the output folder or better yet, if possible, put them inside my one dll/so.

I've been trying to fit bits and pieces of information together from all over, but I can't get it the way I want it.

Finally, I apologize if this is not an appropriate question but I don't know where else to ask. If there is a better place, let me know and I will delete and repost there.