Gå til innhold

Noen som vet om en god kompilerings guide? (C++)


Anbefalte innlegg

Videoannonse
Annonse

Du kan jo bare parse input til GCC via terminal eller du kan ta ibruk Makefiles med pkg-config.

Noe som er en tanke vanskeligere.

 

Eksempel:

 

Makefile:

CC = gcc

 

CFLAGS = -Wall    \

-DG_DISABLE_DEPRECATED    \

-DGDK_DISABLE_DEPRECATED  \

-DGDK_PIXBUF_DISABLE_DEPRECATED \

-DGTK_DISABLE_DEPRECATED

 

radiobuttons: radiobuttons.c

$(CC) radiobuttons.c -o radiobuttons $(CFLAGS) `pkg-config gtk+-2.0 --cflags --libs`

 

clean:

rm -f *.o radiobuttons

 

 

Koden:

#include <glib.h>

#include <gtk/gtk.h>

 

static gboolean close_application( GtkWidget *widget,

                                  GdkEvent  *event,

                                  gpointer  data )

{

  gtk_main_quit ();

  return FALSE;

}

 

int main( int  argc,

          char *argv[] )

{

    GtkWidget *window = NULL;

    GtkWidget *box1;

    GtkWidget *box2;

    GtkWidget *button;

    GtkWidget *separator;

    GSList *group;

 

    gtk_init (&argc, &argv);   

     

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

 

    g_signal_connect (G_OBJECT (window), "delete_event",

        G_CALLBACK (close_application),

                      NULL);

 

    gtk_window_set_title (GTK_WINDOW (window), "radio buttons");

    gtk_container_set_border_width (GTK_CONTAINER (window), 0);

 

    box1 = gtk_vbox_new (FALSE, 0);

    gtk_container_add (GTK_CONTAINER (window), box1);

    gtk_widget_show (box1);

 

    box2 = gtk_vbox_new (FALSE, 10);

    gtk_container_set_border_width (GTK_CONTAINER (box2), 10);

    gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);

    gtk_widget_show (box2);

 

    button = gtk_radio_button_new_with_label (NULL, "button1");

    gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

    gtk_widget_show (button);

 

    group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));

    button = gtk_radio_button_new_with_label (group, "button2");

    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);

    gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

    gtk_widget_show (button);

 

    button = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (button),

                                                  "button3");

    gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

    gtk_widget_show (button);

 

    separator = gtk_hseparator_new ();

    gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);

    gtk_widget_show (separator);

 

    box2 = gtk_vbox_new (FALSE, 10);

    gtk_container_set_border_width (GTK_CONTAINER (box2), 10);

    gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);

    gtk_widget_show (box2);

 

    button = gtk_button_new_with_label ("close");

    g_signal_connect_swapped (G_OBJECT (button), "clicked",

                              G_CALLBACK (close_application),

                              G_OBJECT (window));

    gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

    GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);

    gtk_widget_grab_default (button);

    gtk_widget_show (button);

    gtk_widget_show (window);

   

    gtk_main ();

 

    return 0;

}

 

Det du ser over er innholde i to filer.

"Makefile" og "radiobuttons.c".

Bare en av eksemplene som følger med GTK.

Hvis du har de to filene i en mappe, skriver du bare make i terminal, så sant du er i rett mappe med terminal og c filen kompileres.

(Samme for cpp filer)

Lenke til kommentar

Ok, jeg har denne filen:

Klikk for å se/fjerne innholdet nedenfor
// Name: minimal.cpp

 

// Purpose: Minimal wxWidgets sample

 

// Author: Julian Smart

 

 

 

#include "wx/wx.h"

 

 

 

// Declare the application class

 

class MyApp : public wxApp

 

{

 

public:

 

// Called on application startup

 

virtual bool OnInit();

 

};

 

 

 

// Declare our main frame class

 

class MyFrame : public wxFrame

 

{

 

public:

 

// Constructor

 

MyFrame(const wxString& title);

 

 

 

// Event handlers

 

void OnQuit(wxCommandEvent& event);

 

void OnAbout(wxCommandEvent& event);

 

 

 

private:

 

// This class handles events

 

DECLARE_EVENT_TABLE()

 

};

 

 

 

// Implements MyApp& GetApp()

 

DECLARE_APP(MyApp)

 

 

 

// Give wxWidgets the means to create a MyApp object

 

IMPLEMENT_APP(MyApp)

 

 

 

// Initialize the application

 

bool MyApp::OnInit()

 

{

 

// Create the main application window

 

MyFrame *frame = new MyFrame(wxT("Minimal wxWidgets App"));

 

 

 

// Show it

 

frame->Show(true);

 

 

 

// Start the event loop

 

return true;

 

}

 

 

 

// Event table for MyFrame

 

BEGIN_EVENT_TABLE(MyFrame, wxFrame)

 

EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)

 

EVT_MENU(wxID_EXIT, MyFrame::OnQuit)

 

END_EVENT_TABLE()

 

 

 

void MyFrame::OnAbout(wxCommandEvent& event)

 

{

 

wxString msg;

 

msg.Printf(wxT("Hello and welcome to %s"),

 

wxVERSION_STRING);

 

 

 

wxMessageBox(msg, wxT("About Minimal"),

 

wxOK | wxICON_INFORMATION, this);

 

}

 

 

 

void MyFrame::OnQuit(wxCommandEvent& event)

 

{

 

// Destroy the frame

 

Close();

 

}

 

 

 

#include "mondrian.xpm"

 

 

 

MyFrame::MyFrame(const wxString& title)

 

: wxFrame(NULL, wxID_ANY, title)

 

{

 

// Set the frame icon

 

SetIcon(wxIcon(mondrian_xpm));

 

 

 

// Create a menu bar

 

wxMenu *fileMenu = new wxMenu;

 

 

 

// The "About" item should be in the help menu

 

wxMenu *helpMenu = new wxMenu;

 

helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),

 

wxT("Show about dialog"));

 

 

 

fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),

 

wxT("Quit this program"));

 

 

 

// Now append the freshly created menu to the menu bar...

 

wxMenuBar *menuBar = new wxMenuBar();

 

menuBar->Append(fileMenu, wxT("&File"));

 

menuBar->Append(helpMenu, wxT("&Help"));

 

 

 

// ... and attach this menu bar to the frame

 

SetMenuBar(menuBar);

 

 

 

// Create a status bar just for fun

 

CreateStatusBar(2);

 

SetStatusText(wxT("Welcome to wxWidgets!"));

 

}

 

 

 

Den kommer fra boken:

Cross-Platform GUI Programming with WxWidgets. (Eksempelet i kap 2).

 

Men det følger ikke med noen makefile eller noen instruksjoner om hvordan man kompilerer eksemplene i boken. Ganske håpløst...

Lenke til kommentar

Takker.

 

Her er makefilen til å kompilere programmet fra min forrige post. (Dersom noen som har samme problem skulle støte på denne tråden. Trykk svar på post og kopier, forumet fjerner tabulatorer og ekstra mellomrom fra formatteringen).

 

Klikk for å se/fjerne innholdet nedenfor

PROGRAM = minimal

CXX = $(shell wx-config --cxx)

OBJECTS = $(PROGRAM).o

 

# implementation

.SUFFIXES: .o .cpp

.cpp.o :

$(CXX) -c `wx-config --cxxflags` -o $@ $<

 

all: $(PROGRAM)

$(PROGRAM): $(OBJECTS)

$(CXX) -o $(PROGRAM) $(OBJECTS) `wx-config --libs`

 

clean:

rm -f *.o $(PROGRAM)

Endret av HavarN
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...