Include another CMake project as a library

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

The C source does not have a header, so I created it, and some structures were transferred to it, as well as the declaration of the only function in the C source that I need to call.

The problem is that I cannot call it because either CMake does not find the C file (and therefore the definition), or when I add the C source to my source code installed in CMakeLists.txt, it complains that both my main.cpp and the C source file have basic functions.

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

here he is:

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 do not want to change the source of C too much, since this is an independent project. I guess the developer will accept the introduction of the header, but not much more.

+4
source share
4 answers

I'm a little rusty, but you could do something like this:

At oregengui.c

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

In your CMakeLists you want as in:

 add_definitions( -DMYGUIPROJ ) 

Then the compiler should ignore your second main function.

+1
source

Try compiling the C file as a library and linking it to an executable file.

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

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

0
source

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

0
source

Source: https://habr.com/ru/post/1336943/


All Articles