简体   繁体   English

如何从c ++中的其他文件调用main函数?

[英]How to call function in main from other file in c++?

I'm practicing with gtk (or gtkmm in this case), to which I'm completely new and I'm relatively new to c++. 我正在使用gtk(在这种情况下为gtkmm)进行练习,这是我的新手,并且对c ++还是比较新的。 I got a working program that could open a window and put a few widgets in it, but now I'm trying to add an action to a button, and it just won't work. 我有一个可以打开一个窗口并在其中放入一些小部件的工作程序,但是现在我试图向按钮添加一个动作,但这根本行不通。

main.cc: main.cc:

    #include <iostream>
    #include "buttons.h"
    #include <gtkmm/application.h>

    void printLine()
    {
        std::cout<<"you pressed the button"<<std::endl;
    }

    int main(int argc, char *argv[])
    {
        Glib::RefPtr<Gtk::Application> app =
        Gtk::Application::create(argc, argv,
        "org.gtkmm.examples.base");

        Buttons buttons;


         return app->run(buttons);

    }

buttons.h: button.h:

    #ifndef GTKMM_EXAMPLE_BUTTONS_H
    #define GTKMM_EXAMPLE_BUTTONS_H

    #include <gtkmm/window.h>
    #include <gtkmm/button.h>
    #include <gtkmm/box.h>

    class Buttons : public Gtk::Window
    {
    public:
        Buttons();

        virtual ~Buttons();

    protected:
        //Signal handlers:
        void on_button_clicked();

        //Child widgets:
        Gtk::Button m_button;
        Gtk::Box buttonBox;
    };

    #endif //GTKMM_EXAMPLE_BUTTONS_H 

buttons.cc: button.cc:

    #include <iostream>
    #include "buttons.h"

    Buttons::Buttons()
    {
    m_button.add_pixlabel("info.xpm", "click here");

    set_title("Pixmap'd buttons!");
    set_border_width(10);

    m_button.signal_clicked().connect( sigc::mem_fun(*this,
          &Buttons::on_button_clicked) );

    add(buttonBox);

    buttonBox.pack_start(m_button);

    //m_button.show();
   show_all_children();
   }

   Buttons::~Buttons()
   {

   } 

   void Buttons::on_button_clicked()
   {
   printLine();
   }

I am using g++ to compile the program and it gives me this error message: g++ main.cc -o button pkg-config gtkmm-3.0 --cflags --libs /tmp/ccKyphYe.o: In function main': main.cc:(.text+0x93): undefined reference to Buttons::Buttons()' main.cc:(.text+0xc5): undefined reference to Buttons::~Buttons()' main.cc:(.text+0x124): undefined reference to Buttons::~Buttons()' collect2: error: ld returned 1 exit status 我正在使用g ++编译程序,它给了我此错误消息:g ++ main.cc -o按钮pkg-config gtkmm-3.0 --cflags --libs /tmp/ccKyphYe.o:在函数main': main.cc:(.text+0x93): undefined reference to Buttons :: Buttons()的main.cc:(.text+0xc5):未定义对Buttons::~Buttons()' main.cc:(.text+0x124): undefined reference to Buttons ::〜Buttons()'的引用collect2:错误:ld返回1退出状态

You have to put all your source files in the compile line, so just add buttons.cc right after main.cc and you should be good. 您必须将所有源文件放在编译行中,因此只需在main.cc之后添加buttons.cc,就可以了。 There are other ways to do it, but just to get you going, that should work. 还有其他方法可以做到,但只是为了让您前进,这应该行得通。

The longer answer is that the compiler compiles each src file (.cc files in your example) separately and builds object files (.o or .obj). 更长的答案是编译器分别编译每个src文件(在您的示例中为.cc文件)并构建目标文件(.o或.obj)。 To do this, all it needs are the declarations of the things it uses (#include'd in header files). 为此,它所需要的只是它使用的东西的声明(在头文件中包含#include)。 If they are missing, you get a "compiler error". 如果缺少它们,则会出现“编译器错误”。

But later when it actually puts together the final program that you are going to run, it needs the actual definitions (the actual code) for everything that is used, and if it can't find the actual definition, you get "undefined reference" errors. 但是稍后,当它实际上将要运行的最终程序放到一起时,它需要使用的所有内容的实际定义(实际代码),并且如果找不到实际定义,则会得到“未定义引用”错误。 This is called a "linker error". 这称为“链接器错误”。 This means you are missing libraries, archives, or object (.obj) files. 这意味着您缺少库,档案或对象(.obj)文件。

HOWEVER, when you put everything on the same compiler line -- all your c++ src files including one with a main() function, the compiler automatically generates the object files and does the linking all in one step. 但是,当您将所有内容放在同一编译器行上时—所有c ++ src文件(包括一个带有main()函数的文件),编译器都会自动生成目标文件,并一步一步完成所有链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM