4

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.

flying sheep
  • 8,475
  • 5
  • 56
  • 73

4 Answers4

2

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)
Maciej
  • 3,685
  • 1
  • 19
  • 14
1

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.

badgerr
  • 7,802
  • 2
  • 28
  • 43
0

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.

tibur
  • 11,531
  • 2
  • 37
  • 39
  • But then the C project won’t compile into an executable anymore, since it doesn’t have a main function… – flying sheep Jan 25 '11 at 12:40
  • Just add the C file as part of your executable, and rename its main function to dummy_main, for instance. You will be able to use that function from your main, or from your Qt stuff. – tibur Jan 25 '11 at 13:18
  • I know, but I only want to refactor the C file, so that it compiles both as standalone and as part of my project, without changing anything. – flying sheep Jan 25 '11 at 14:21
  • You can't. You need to have only one main function in your executable. If you don't want to modify the C file, then you have to check Arun answer about creating a new process. – tibur Jan 25 '11 at 14:41
0

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?

Sulla
  • 7,631
  • 9
  • 45
  • 71
  • 1
    I don’t even use the output. On the other hand, it isn’t very elegant, since i need the structures from the C header anyway, and I figured that using a function wouldn’t very far from there. – flying sheep Jan 25 '11 at 12:42