이 지침서에서는 다음 방법을 보여줍니다:
GTK+로 간단한 "Hello, World" 프로그램을 만듭니다
빌드 시스템 구성 방법
class MyApplication : Gtk.Application {
protected override void activate () {
var window = new Gtk.ApplicationWindow (this);
window.set_title ("Welcome to GNOME");
window.set_default_size (200, 100);
window.show_all ();
}
}
Gtk.Application은 GTK+를 초기화합니다. 또한 창을 만들 때 자동으로 붙인
첫 창 만들기를 시작할 수 있습니다. window 변수를 만들어 Gtk.ApplicationWindow를 할당하겠습니다.
set_title
로 창 제목을 설정하겠습니다. 여러분이 원하는대로 제목을 지을 수 있습니다. 안전한 방편으로 UTF-8 인코딩 방식으로 제목을 넣으시는게 좋습니다.
이제 제목과 동작하는 "닫기" 단추가 붙은 창을 만들었습니다. 이제 실제 "Hello World" 문구를 찍어보겠습니다.
var label = new Gtk.Label ("Hello GNOME!");
window.add (label);
마지막으로 프로그램을 만들고 실행하겠습니다:
int main (string[] args) {
return new MyApplication ().run (args);
}
Gtk.ApplicationWindow는 한번에 위젯 하나만 가질 수 있습니다. 프로그램을 더 정교하게 만들려면 Gtk.Grid 같은 홀더 위젯을 창 안에 만들어 넣고 그 안에 위젯을 추가해야합니다.
완전한 파일 내용:
public class MyApplication : Gtk.Application {
protected override void activate () {
var window = new Gtk.ApplicationWindow (this);
var label = new Gtk.Label ("Hello GNOME!");
window.add (label);
window.set_title ("Welcome to GNOME");
window.set_default_size (200, 100);
window.show_all ();
}
}
public int main (string[] args) {
return new MyApplication ().run (args);
}
이 프로그램을 실행하려면 우선 hello-world.vala로 저장하십시오. 터미널을 연 다음 프로그램을 저장한 폴더로 이동하십시오.
프로그램을 컴파일하시고요:
프로그램을 실행하시죠:
터미널에서의 프로그램 실행은 프로그램을 처음 만드는 단계에서 상당히 유용합니다. 그놈 3에서 완벽하게 프로그램 통합하여 동작할 수 있게 하려면 데스크톱 실행 아이콘이 필요합니다. 이 아이콘을 만들려면
예제에서는 .desktop.in
파일에서 최소한 필요한 내용을 보여줍니다.
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=Hello World
Comment=Say Hello
Exec=@prefix@/bin/hello-world
Icon=application-default-icon
Terminal=false
Type=Application
StartupNotify=true
Categories=GNOME;GTK;Utility;
이제 .desktop.in
파일 부분으로 들어가보겠습니다.
프로그램 이름입니다.
프로그램의 간단한 설명입니다.
메뉴에서 프로그램을 선택했을 때 실행할 명령을 지정합니다. 이 예제에서는
Exec 키의 명령을 터미널 창에서 실행할 지 여부를 지정합니다.
프로그램을 적당한 분류에 넣으려면 Categories 줄에 필요한 분류 이름을 추가해야합니다. 다른 분류에 대한 자세한 정보는 메뉴 명세에 있습니다.
이 예제에서는 이미 있는 아이콘을 사용하겠습니다. 개별 아이콘을 사용하려면
그놈 3의 일부 프로그램을 만들려면 autotools의 도움을 받아 설치해야합니다. autotools 빌드는 필요한 모든 파일을 모든 올바른 경로에 설치합니다.
진행하려면 다음 파일이 필요합니다:
#!/bin/sh
set -e
test -n "$srcdir" || srcdir=`dirname "$0"`
test -n "$srcdir" || srcdir=.
olddir=`pwd`
cd "$srcdir"
# This will run autoconf, automake, etc. for us
autoreconf --force --install
cd "$olddir"
if test -z "$NOCONFIGURE"; then
"$srcdir"/configure "$@"
fi
# The actual runnable program is set to the SCRIPTS primitive.
# # Prefix bin_ tells where to copy this
bin_PROGRAMS = hello-world
hello_world_CFLAGS = $(gtk_CFLAGS)
hello_world_LDADD = $(gtk_LIBS)
hello_world_VALAFLAGS = --pkg gtk+-3.0
hello_world_SOURCES = hello-world.vala
desktopdir = $(datadir)/applications
desktop_DATA = \
hello-world.desktop
# This file is processed by autoconf to create a configure script
AC_INIT([Hello World], 1.0)
AM_INIT_AUTOMAKE([1.10 no-define foreign dist-xz no-dist-gzip])
AC_PROG_CC
AM_PROG_VALAC([0.16])
PKG_CHECK_MODULES(gtk, gtk+-3.0)
AC_CONFIG_FILES([Makefile hello-world.desktop])
AC_OUTPUT
사용자가 우선 읽어야 할 내용입니다. 이 파일은 비워둘 수 있습니다.
제대로 된 정보를 올바르게 갖춘
To build and install this program:
./autogen.sh --prefix=/home/your_username/.local
make
make install
-------------
Running the first line above creates the following files:
aclocal.m4
autom4te.cache
config.log
config.status
configure
depcomp
hello-world
hello-world.c
hello-world.desktop
hello_world-hello-world.o
hello_world_vala.stamp
install-sh
missing
Makefile.in
Makefile
Running "make" links all the appropriate libraries.
Running "make install", installs the application in /home/your_username/.local/bin
and installs the hello-world.desktop file in /home/your_username/.local/share/applications
You can now run the application by typing "Hello World" in the Overview.
----------------
To uninstall, type:
make uninstall
----------------
To create a tarball type:
make distcheck
This will create hello-world-1.0.tar.xz