简体   繁体   中英

Include another CMake Project as Library

I have decided to make a C++-Qt-GUI for a C program (both languages I don't know), using KDevelop, which in turn uses CMake.

The C source has no header, so I made one, and migrated some structs into it, as well as the declaration of the only function in the C source i need to call.

The problem is that I can't call it, because either CMake doesn't find the C file (and hence the definition), or, when I add the C source to my source set in the CMakeLists.txt, it complains that both my main.cpp and the C source file have main functions.

How do I tell CMake that it should only make the function from the C file available which I declared in the header?

here is it:

project(oregengui)
cmake_minimum_required(VERSION 2.6)
find_package(Qt4 REQUIRED)

include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})

set(oregengui_SRCS oregengui.cpp main.cpp qrangeslider/qrangeslider.cpp)
#as soon as i add oregengui/oregengui.c, it won’t work because of the main function
qt4_automoc(${oregengui_SRCS})
add_executable(oregengui ${oregengui_SRCS})
target_link_libraries(oregengui ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})

PS: I don't want to change the C source too much, since it is a independant project. I guess the developer will accept the introduction of the header, but not much more.

Try compiling C file as library and linking it with executable.

add_library(clib oregengui/oregengui.c)
[...]
add_executable(oregengui ${oregengui_SRCS})
target_link_libraries(oregengui ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} clib)

I'm a bit rusty on this, but could you do something along the lines of:

In oregengui.c

#if !defined MYGUIPROJ
int main() {
...
}
#endif

In your CMakeLists that you want both in:

add_definitions( -DMYGUIPROJ )

The compiler should then ignore your second main function.

Your program cannot contain two main functions at the same time. Just try to rename the main function in the C file you included to your project. This is not a big change to the source, but you will have to do it locally.

if your C program has a main function, then it can be executed independently. So why not use the QProcess::execute() method to invoke the application from qt and get the output?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM