solusipse
solusipse

Reputation: 587

qmake: compile single cpp file into an executable

I got a project in QT and I'd like to use qmake for compiling one additional cpp file that (into standalone executable) is not connected in any way to my application or even QT (it's very simple plain C++ program). Is there any way to do this without rebulding whole project structure? Do I need separate .pro file for every executable or is there any other way for simple compiling just one, plain C++ file?

Upvotes: 1

Views: 1308

Answers (2)

svlasov
svlasov

Reputation: 10456

To make an additional executable you can use use system() in .pro like so:

system(g++ otherapp.cpp)

Which will be built every time you call qmake. However if you want to build the additional app automatically when its source is changed, use QMAKE_EXTRA_TARGETS instead.

Upvotes: 1

youssef
youssef

Reputation: 623

As you may know qmake -project will make one .pro file with the name of the folder containing your whole source and header files, if you qmake this pro file then make your project you will get compiled .o file from your new cpp file even if it's not connect to your Qt project directly.

but if this file got main() function of course you will have multiple main() definitions error by compiler.

you will need to rebuild that file of course

as you know for simple compiling of only one standard plain c++ file you just

g++ source.cpp -o excutable_name.exe

for more strict compiling with two steps:

g++ -Wall -pedantic -ansi source.cpp -c compiled_file_name.o
g++  compiled_file_name -o excutable_name.exe

but if you are going to use for example a code related to Qt, you have to include Qt headers and link necessary libraries :

g++ -Wall source.cpp -c compiled_file_name.o -L qt/library/path -lQtGui -lQtCore  -lQtother_necessary_libraries -I Qt/include/path -I Qtother_necessary_include_paths

Upvotes: 3

Related Questions