Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Generate a makefile without a main function from Qmake - "undefined reference to `main' "
Forum Update on Monday, May 27th 2025

Generate a makefile without a main function from Qmake - "undefined reference to `main' "

Scheduled Pinned Locked Moved Solved General and Desktop
qmake makefilemainprotobuf
12 Posts 4 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N NikoBir

    Hello everyone !

    I'm currently trying to generate protobuf files with qmake.
    But when I execute the makefile, I have :

    • My correctly generated protobuf files (yes !).
    • An error : "undefined reference to `main' "

    I don't want to have a main function, I just want to generate some files. So, I don't need to have a main function .... But Qmake really want it....
    How can I explain him that it's just fine and cool ?

    I tested to change my TEMPLATE in my .pro but it changes nothing ...

    These files are in the same folder :

    My .pro :

    QT -= gui
    TEMPLATE += lib
    INCLUDEPATH += /usr/include/
    LIBS += /usr/lib/libprotobuf.so
    PROTOS = comGRPC.proto
    include(protobuf.pri)
    

    My .pri :

    PROTOPATH += .
    PROTOPATH += ../Protocol
    PROTOPATHS =
    for(p, PROTOPATH):PROTOPATHS += --proto_path=$${p}
    
    protobuf_decl.name = protobuf header
    protobuf_decl.input = PROTOS
    protobuf_decl.output = ${QMAKE_FILE_BASE}.pb.h
    protobuf_decl.commands = /usr/bin/protoc --cpp_out="." $${PROTOPATHS} ${QMAKE_FILE_NAME}
    protobuf_decl.variable_out = GENERATED_FILES
    QMAKE_EXTRA_COMPILERS += protobuf_decl
    
    protobuf_impl.name = protobuf implementation
    protobuf_impl.input = PROTOS
    protobuf_impl.output = ${QMAKE_FILE_BASE}.pb.cc
    protobuf_impl.depends = ${QMAKE_FILE_BASE}.pb.h
    protobuf_impl.commands = $$escape_expand(\n)
    protobuf_impl.variable_out = GENERATED_SOURCES
    QMAKE_EXTRA_COMPILERS += protobuf_impl
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @NikoBir said in Generate a makefile without a main function from Qmake - "undefined reference to `main' ":

    How can I explain him that it's just fine and cool ?

    What exact pro file do you build?
    If it is My.pro then it should work as it is a library pro file.
    Also, did you change TEMPLATE to lib or was it always a lib?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • N Offline
      N Offline
      NikoBir
      wrote on last edited by
      #3

      Thanks for your answer, yes it's my pro file that I want to build.
      I added TEMPLATE += lib because I read that I need to use this variable if I want to build without a main function.

      jsulmJ 1 Reply Last reply
      0
      • N NikoBir

        Thanks for your answer, yes it's my pro file that I want to build.
        I added TEMPLATE += lib because I read that I need to use this variable if I want to build without a main function.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @NikoBir said in Generate a makefile without a main function from Qmake - "undefined reference to `main' ":

        I added TEMPLATE += lib because I read that I need to use this variable if I want to build without a main function

        That is clear. My question is whether you had something else there and then changed to lib. If that's the case then delete everything in build folder, run qmake and build.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        SGaistS 1 Reply Last reply
        0
        • N Offline
          N Offline
          NikoBir
          wrote on last edited by
          #5

          Ah ! I misunderstood ! Yes, I had nothing before. I even didn't had a TEMPLATE variable.
          Like you said I removed everything in the build folder, ran qmake and built but I have the same issue.

          1 Reply Last reply
          0
          • N NikoBir

            Hello everyone !

            I'm currently trying to generate protobuf files with qmake.
            But when I execute the makefile, I have :

            • My correctly generated protobuf files (yes !).
            • An error : "undefined reference to `main' "

            I don't want to have a main function, I just want to generate some files. So, I don't need to have a main function .... But Qmake really want it....
            How can I explain him that it's just fine and cool ?

            I tested to change my TEMPLATE in my .pro but it changes nothing ...

            These files are in the same folder :

            My .pro :

            QT -= gui
            TEMPLATE += lib
            INCLUDEPATH += /usr/include/
            LIBS += /usr/lib/libprotobuf.so
            PROTOS = comGRPC.proto
            include(protobuf.pri)
            

            My .pri :

            PROTOPATH += .
            PROTOPATH += ../Protocol
            PROTOPATHS =
            for(p, PROTOPATH):PROTOPATHS += --proto_path=$${p}
            
            protobuf_decl.name = protobuf header
            protobuf_decl.input = PROTOS
            protobuf_decl.output = ${QMAKE_FILE_BASE}.pb.h
            protobuf_decl.commands = /usr/bin/protoc --cpp_out="." $${PROTOPATHS} ${QMAKE_FILE_NAME}
            protobuf_decl.variable_out = GENERATED_FILES
            QMAKE_EXTRA_COMPILERS += protobuf_decl
            
            protobuf_impl.name = protobuf implementation
            protobuf_impl.input = PROTOS
            protobuf_impl.output = ${QMAKE_FILE_BASE}.pb.cc
            protobuf_impl.depends = ${QMAKE_FILE_BASE}.pb.h
            protobuf_impl.commands = $$escape_expand(\n)
            protobuf_impl.variable_out = GENERATED_SOURCES
            QMAKE_EXTRA_COMPILERS += protobuf_impl
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @NikoBir said in Generate a makefile without a main function from Qmake - "undefined reference to `main' ":

            LIBS += /usr/lib/libprotobuf.so

            This line is wrong please seee here how to do it properly: https://doc.qt.io/qt-6/qmake-variable-reference.html#libs), but not the cause for your issue I think.
            Can you provide a minimal reproducible project, so others can try?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • N Offline
              N Offline
              NikoBir
              wrote on last edited by
              #7

              Thank you, yes I can !
              I created this repository with my zipped project. I added the includes and libs.

              https://github.com/QtForumHelp/undefined_reference_to_main_Qmake-Protobuf

              jsulmJ 1 Reply Last reply
              0
              • N NikoBir

                Thank you, yes I can !
                I created this repository with my zipped project. I added the includes and libs.

                https://github.com/QtForumHelp/undefined_reference_to_main_Qmake-Protobuf

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @NikoBir I will check (it does not build for me). But your LIBS is still wrong...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NikoBir
                  wrote on last edited by
                  #9

                  Ok I will correct it !

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @NikoBir said in Generate a makefile without a main function from Qmake - "undefined reference to `main' ":

                    I added TEMPLATE += lib because I read that I need to use this variable if I want to build without a main function

                    That is clear. My question is whether you had something else there and then changed to lib. If that's the case then delete everything in build folder, run qmake and build.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by SGaist
                    #10

                    Hi,

                    @jsulm said in Generate a makefile without a main function from Qmake - "undefined reference to `main' ":

                    @NikoBir said in Generate a makefile without a main function from Qmake - "undefined reference to `main' ":

                    I added TEMPLATE += lib because I read that I need to use this variable if I want to build without a main function

                    That is clear. My question is whether you had something else there and then changed to lib. If that's the case then delete everything in build folder, run qmake and build.

                    In addition to what @jsulm, TEMPLATE should contain a single value. Here you likely have something else. So just in case, replace it with TEMPLATE = lib.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Paul ColbyP 1 Reply Last reply
                    3
                    • SGaistS SGaist

                      Hi,

                      @jsulm said in Generate a makefile without a main function from Qmake - "undefined reference to `main' ":

                      @NikoBir said in Generate a makefile without a main function from Qmake - "undefined reference to `main' ":

                      I added TEMPLATE += lib because I read that I need to use this variable if I want to build without a main function

                      That is clear. My question is whether you had something else there and then changed to lib. If that's the case then delete everything in build folder, run qmake and build.

                      In addition to what @jsulm, TEMPLATE should contain a single value. Here you likely have something else. So just in case, replace it with TEMPLATE = lib.

                      Paul ColbyP Offline
                      Paul ColbyP Offline
                      Paul Colby
                      wrote on last edited by
                      #11

                      @SGaist wrote:

                      TEMPLATE should contain a single value. Here you likely something else. So just in case, replace it with TEMPLATE = lib.

                      Indeed, as per the docs, TEMPLATE defaults to app. So by using += the TEMPLATE ends up being app lib, which is not what you want :)

                      So, as @SGaist indicated, drop the + from that line so:

                      TEMPLATE = lib
                      

                      Cheers.

                      1 Reply Last reply
                      2
                      • N Offline
                        N Offline
                        NikoBir
                        wrote on last edited by
                        #12

                        Thank you all ! It worked !!!!!

                        Yes the solution was TEMPLATE = lib !

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved