• moc处理cpp文件


    在使用qt的时候,常常为了实现的需求将一些类隐藏在cpp中文件实现,而这些类又需要一些qt自己的机制支持如Q_OBJECT宏。于是在编译的时候,很可能出现像undefined reference to vtable for "xxx::xxx"的问题,这其实是由于qt不会自动moc cpp文件。参考qt的文档,发现最简单的方法就是用qmake重新生成makefile文件就可以了。另外就是直接把该类写在.h文件里面在编译。

    如果需要自己重新写makefileqt文档给出了以下的方法

    For Q_OBJECT class declarations in header files, here is a useful makefile rule if you only use GNU make:

     moc_%.cpp: %.h

             moc $(DEFINES) $(INCPATH) $< -o $@

    If you want to write portably, you can use individual rules of the following form:

     moc_foo.cpp: foo.h

             moc $(DEFINES) $(INCPATH) $< -o $@

    You must also remember to add moc_foo.cpp to your SOURCES (substitute your favorite name) variable and moc_foo.o or moc_foo.obj to your OBJECTS variable.

    Both examples assume that $(DEFINES) and $(INCPATH) expand to the define and include path options that are passed to the C++ compiler. These are required by moc to preprocess the source files.

    While we prefer to name our C++ source files .cpp, you can use any other extension, such as .C, .cc, .CC, .cxx, and .c++, if you prefer.

    For Q_OBJECT class declarations in implementation (.cpp) files, we suggest a makefile rule like this:

     foo.o: foo.moc

     

     foo.moc: foo.cpp

             moc $(DEFINES) $(INCPATH) -i $< -o $@

    This guarantees that make will run the moc before it compiles foo.cpp. You can then put

     #include "foo.moc"

     

    对于.cpp文件里面的Q_OBJECT,我们只需在makefile文件里面添加

    foo.o: foo.moc

     

     foo.moc: foo.cpp

             moc $(DEFINES) $(INCPATH) -i $< -o $@

    这样的规则就可以了,然后在.cpp文件的末尾写上

    #include "foo.moc"

    这样就能够正确的编译,不过我最推荐的还是用qmake重新生成makefile

    另外,对于qtopia,我们使用qtopiamake重新生成makefile,但是我在qtopiamake的时候出现了一点问题,就是重新编译的时候还是出错,不过如果首先make clean,然后在删除makefile,在使用qtopiamake,然后在make就不会出错。而我在qt下就直接用qmakemake,不会出错。可能是因为qtopiamake并没有删除原来生成的.obj文件的原因吧。

     
  • 相关阅读:
    Dungeon Master(BFS)
    Shuffle'm Up(串)
    分解素因子
    Snowflake Snow Snowflakes(查找)
    求素数(素数筛选+打表)
    A simple problem(并查集判环)
    Eqs(枚举+ hash)
    Squares(枚举+set 查找)
    从控制台输入10个人的年龄放入数组,将十个人的年龄求总和
    数组
  • 原文地址:https://www.cnblogs.com/xiaowangba/p/6313803.html
Copyright © 2020-2023  润新知