c++ - Why does AUTOMOC fail, if both find_package() and qt5_use_modules() are called from functions? -


i trying compile qt project, including qcustomplot library. minimum example, set project consisting of:

qcustomplot.h qcustomplot.cpp cmakelists.txt ../cmake/qcustomplot.cmake 

the original project bigger, problem reproducible files.

the cmakelists.txt contains code:

cmake_minimum_required(version 3.6)  set(cmake_automoc on) include(../cmake/qcustomplot.cmake)  function(findqt)                                       #(1)   find_package(qt5core)   find_package(qt5gui)   find_package(qt5widgets)   find_package(qt5printsupport) endfunction()                                          #(1)  findqt()                                               #(1) #find_package(qt5core)                                 #(2)  add_library(   plots   src/qcustomplot.h   src/qcustomplot.cpp )   function(linkqt)                                       #(3)   qt5_use_modules(plots core gui widgets printsupport) endfunction()                                          #(3) linkqt()                                               #(3) 

if either lines marked (1) or lines marked (3) commented out, means calling find_package() or qt5_use_modules() @ file scope, project containing 2 qcustomplot files , additional plots_automoc.cpp. additional file autogenerated , contains necessary #include "moc_qcustomplot.cpp" , project compiles , links properly.

however move qt related commands functions, automoc.cpp file no longer generated , no longer part of project, leads quite few unresolved external symbol during linking.

calling single find_package() on files scope (like line (2)) resolves issue , generates automoc file again.

why moving calls function change automoc behaviour , how can achieve still move them functions?

a little bit background: have quite few targets in project, expect number grow rapidly , want avoid code redundancy. people not trained cmake supposed use it. thats why i'm trying move qt related commands functions , provide command this:

add_my_target(   targetname   sources qcustomplot.h qcustomplot.cpp   qt core gui widgets printsupport   boost filesystem ) 

with exception of qt, achieved this...

i tried using

target_link_libraries(${projectname} ${qt5_core_libraries} ... //or target_link_libraries(${projectname} qt5::core ... 

it leads same result, long not @ least 1 find_package() call directly on files scope, no automoc generated.

i using cmake 3.6.2, qt 5.7, visual studio 2015 , win 10.

additional example olen got of underlying confusion sorted out answer, 1 case remains:

cmake_minimum_required(version 3.6) set(cmake_automoc on)  function(doit)                        #(4)   find_package(qt5core)    add_library(     plots     qcustomplot.h     qcustomplot.cpp     )    qt5_use_modules(plots core gui widgets printsupport) endfunction()                         #(4)  doit()                                #(4) 

using lines marked (4) put generation of target function allows cmake run properly, i.e. qt5_use_modules() defined, able find modules , link them (e.g. include directories set properly). outside function rely on variables set of qt functions. still automoc not generate needed .cpp file. commenting marked lines out runs automoc again.

the problem cmake functions introduce scope, variables defined in find_package not available outside function.

an easy solution problem use cmake macros - can think of them cmake equivalent of c macros. variables declared there available outside macro scope.

in case, means replacing function macro , endfunction endmacro.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -