Pregunta

I am trying to use CMake with Qt and LuaJIT that will run on Visual Studio 2012. I managed somehow to run Qt, but i don't know how to add LuaJIT library to project. I am using source of LuaJIT cloned from http://luajit.org/git/luajit-2.0.git, which is build by running .bat file.

I dont care that LuaJIT will be build by CMake, i just need to link library and add headers to project.

I removed lib folder from my project... Is not worth troubles to have dependancies coupled with project whitout cmake file :D

My project hierarchy is:

+lib
  -luajit-2.0
+src
  -my sources
+ui
  -ui files
-CMakeLists.txt

And CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 2.8.12)

set(PROJECT "Grapedit")

project(${PROJECT})

# Qt Stuff
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

find_package(Qt5Widgets REQUIRED)

set(SOURCE_FILES
  src/main.cpp
  src/mainwindow.h
  src/mainwindow.cpp
)

set(UI_FILES
  ui/mainwindow.ui
)
source_group("UI Files" FILES ${UI_FILES})

qt5_wrap_ui(UI_HEADERS ${UI_FILES})
source_group("Generated UI Headers" FILES ${UI_HEADERS})

add_executable(${PROJECT} ${SOURCE_FILES} ${UI_HEADERS} ${UI_FILES})

qt5_use_modules(${PROJECT} Widgets)

My solution

So it is finally working and I made couple of newbie mistakes... :) I will write them down for others:

  • didn't know what is find module... This will search environment and set up locations of libraries or flag that it didn't find them. Since LuaJIT is compatible with Lua51 you can use find_package(Lua51).
  • Your libraries must be some way visible to CMake. On Windows simplest way is to add them to PATH variable. Or you can add path of your libraries to CMake variable CMAKE_PREFIX_PATH. Open find module, for example FindLua51.cmake and you will see how must be your library organized. On windows I've must installed LuaJIT manualy - created LuaJIT folder and I've put *.h files to include subfolder, *.dll to bin subfolder and *.lib to lib subfolder. Then add bin folder to PATH and set LUA_DIR to LuaJIT folder.
  • use include_directories on include folder
  • then you must link libraries target_link_libraries, but after add_executable!

My CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.12)

# Declare project variables...

set (PROJECT "Grapedit")

set (
    SOURCE_FILES
    src/main.cpp
    src/mainwindow.h
    src/mainwindow.cpp
)

set(UI_FILES
    ui/mainwindow.ui
)

# Set project name
project(${PROJECT})

# Include Lua directories
include_directories(${LUA_INCLUDE_DIR})

# Qt Stuff
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

# Find packages...

# Will find also LuaJIT, but must be named same as Lua51 and installed into directories
find_package(Lua51)

# Find Qt modules, every module separately
find_package(Qt5Widgets REQUIRED)

# Create nice groups in IDEs
source_group("UI Files" FILES ${UI_FILES})
source_group("Generated UI Headers" FILES ${UI_HEADERS})

# Use Qt UI files
qt5_wrap_ui(UI_HEADERS ${UI_FILES})

# Create executable
add_executable (
    ${PROJECT} 
    ${SOURCE_FILES} 
    ${UI_HEADERS} 
    ${UI_FILES}
)

# Link libraries... 
# Must be after executable is created!

# Link Qt modules
qt5_use_modules (
    ${PROJECT} 
    Widgets
)

# Link Lua
target_link_libraries(${PROJECT} ${LUA_LIBRARIES})

# Will not show new windows prompt when running program
if (MSVC)
  set_target_properties(${PROJECT} PROPERTIES
    WIN32_EXECUTABLE YES
    LINK_FLAGS "/ENTRY:mainCRTStartup"
  )
endif ()
¿Fue útil?

Solución

You are missing the actual linkage which you can amend with the following statement:

target_link_libraries(${PROJECT} luajit-5.1)

For sure, it would be even better if this lua jit could have a cmake find module, or config/version file depending on its exact build system.

You could grab the find module from here:

https://github.com/brimworks/lua-zlib/blob/master/cmake/Modules/FindLuaJIT.cmake

Then you could link against it as follows:

target_link_libraries(${PROJECT} ${LUA_LIBRARIES})

You can see that it would become more dynamic this way rather than hard-coding the exact name. The details for figuring out that would be left with the find module.

Note that you would probably need to use the corresponding variables for the header inclusion then as follows:

include_directories(${LUA_INCLUDE_DIR})

This will take care of automatically finding the include directory, respectively, without you hard-coding it.

You would also need to add the following line into your CMakeLists.txt:

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

and you need to place the downloaded find module into a "cmake" subfolder.

Please refer to the following page for further details about this topic in general:

CMake:How To Find Libraries

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top