Archive:Getting Started/Set up KDE 4 for development (zh CN): Difference between revisions

From KDE TechBase
No edit summary
No edit summary
Line 426: Line 426:
:If you have more than one processor or if you have access to an icecream cluster, you might want to check the ''Run multiple jobs'' option and set the ''Number of simultaneous jobs'' to the number of available processors. This increases the compile speed. It is the same as the <tt>-j</tt> option for ''make''.
:If you have more than one processor or if you have access to an icecream cluster, you might want to check the ''Run multiple jobs'' option and set the ''Number of simultaneous jobs'' to the number of available processors. This increases the compile speed. It is the same as the <tt>-j</tt> option for ''make''.


* ''Formatting''
* ''格式(Formatting)''
:You should set all options here to match the coding style of the project you are working on.
:You should set all options here to match the coding style of the project you are working on.


Line 441: Line 441:


Now, open any source file if none is open already. This will enable the ''Settings->Configure Editor...'' entry, where you need to set the tab options to match the tab style used by the project you are working on. The important settings are:
Now, open any source file if none is open already. This will enable the ''Settings->Configure Editor...'' entry, where you need to set the tab options to match the tab style used by the project you are working on. The important settings are:
* ''Appearance->Borders->Show line numbers'': Should be checked.
* ''Appearance->Borders->Show line numbers'': 要检查。
* ''Appearance->Borders->Show icon border'': Should be checked.
* ''Appearance->Borders->Show icon border'': 要检查。
* ''Editing->Tabulators''  
* ''Editing->Tabulators''  
* ''Editing->Static Word Wrap->Show marker'': Should be checked
* ''Editing->Static Word Wrap->Show marker'': 要检查。
* ''Indentation->Automatic Indentation->Indentation mode'': Should be ''C Style''
* ''Indentation->Automatic Indentation->Indentation mode'': 应该是''C Style''
* ''Indentation'' in general
* ''Indentation'' 整体介绍


在主窗口, 点击下部的''CTags''插页, 然后选''Regenerate''按钮来创建一个CTags数据库;这样可以方便的在代码中穿梭来回。
在主窗口, 点击下部的''CTags''插页, 然后选''Regenerate''按钮来创建一个CTags数据库;这样可以方便的在代码中穿梭来回。
Line 458: Line 458:
===== 调试 =====
===== 调试 =====


KDE apps have many symbols, which means that you need a lot of memory to get a decent loading times for debugging. To quote a GDB developer: "I would be reluctant to debug KDE on something with <1GB RAM."
KDE有很多符号,这就意味着在调试时,你需要大内存。引用一个GDB开发员的话语: "在低于1GB内存的机器上调试KDE程序是件很讨厌的事情。"
If the stepping function of the debugger is slow for you, try the following tips:
如果调试的确很慢,试试下列技巧:
* Hide local variables. The <tt>Locals</tt> part of the variable tab on the left causes a big slowdown when stepping if you have many local variables. Simple collapse the <tt>Locals</tt> part of the tree, the local variables are then not updated every step. You can still examine variables by using the <tt>Evaluate expression</tt> function.
* 隐藏本地变量。The <tt>Locals</tt> part of the variable tab on the left causes a big slowdown when stepping if you have many local variables. Simple collapse the <tt>Locals</tt> part of the tree, the local variables are then not updated every step. You can still examine variables by using the <tt>Evaluate expression</tt> function.
* Use the patch at http://bugs.kde.org/show_bug.cgi?id=143977. It prevents the update of the framestack widget at each step, speeding up stepping considerably. The patch introduces some minor glitches, which is why it is not yet commited.
* 用这个补丁:http://bugs.kde.org/show_bug.cgi?id=143977。It prevents the update of the framestack widget at each step, speeding up stepping considerably. The patch introduces some minor glitches, which is why it is not yet commited.


{{Note|
{{Note|
Line 470: Line 470:


{{tip|使用类库前,一定要先安装,然后你才能测试和调试。
{{tip|使用类库前,一定要先安装,然后你才能测试和调试。
Since this is cumbersome and time consuming, you should create symlinks ''(ln -s)'' pointing from the build directory to the installation directory for all affected libraries.
这个很麻烦也很耗时间, 你应该对所有可能引用到的类库建立符号链接(''(ln -s)''),把它们从构建目录连到安装目录。


Often, even simple programs use libraries internally, for example the settings dialog of Konsole is really a library.}}
通常,最简单的程序也会在其中调用一些类库,例如Konsole就是一种类库。}}


[[Category:KDE4]]
[[Category:KDE4]]

Revision as of 02:50, 27 September 2009


Getting_Started/Set_up_KDE_4_for_development

为后续开发设置KDE 4
Tutorial Series   开始
Previous   构建KDE 4
What's Next   其他开发相关主题
Further Reading   n/a
Note
本文假定您已经基于从源代码构建KDE4的介绍成功构建了kdelibs、kdepimlibs和kdebase


启动KDE4的应用程序和会话

当您开始KDE4的开发时,通常您可以有这样三个选择:


随后的章节描述的就是这三种选择。

Note
如果您在运行任何KDE4应用程序时遇到错误,例如:
 Qt: Session management error: Could not open
network socket

(会话管理错误:打不开套接字)

 QMutex::lock: Deadlock detected in thread
-1241241936 

(在线程-1241241936中侦测到死锁)

或者卡在运行startkde的时候,请阅读这篇文章来绕过这个问题。


启动KDE4应用程序

在普通的shell里使用sux

使用这个方法需要sux工具 (http://fgouget.free.fr/sux/sux-readme.shtml)。sux工具在大多数发行版上是可用的。当然你也可以参考下一篇 normal shell without sux 的文档。sux可以让您用一种干净而简洁的方式在切换到其他用户帐号时隐式地管理X转发(认证和显示输出)。

登录时请输入 sux - kde-devel

您的.bashrc脚本将帮助您正确地设置全部环境变量和其他相关配置。如果要启动一个应用程序,只需简单地输入程序名称;例如kwrite

Note
如果您遇到诸如MIME类型丢失的错误,请尝试:
  • 运行 unset XDG_DATA_DIRS ; kbuildsycoca4


在shell里不用SUX

启动KDE 4应用程序最简单的方法是用su作为kde-devel用户登录,然后从命令行简单启动。登录,输入 su - kde-devel, 然后,输入你的密码 export DISPLAY=:0

Note
导入DISPLAY 变量是必须的,这样KDE4程序才能在KDE3桌面显示并运行.

所有的环境变量和其他设置应该参照下面的连接设置正确 .bashrc. 如果想要启动一个程序其实很简单,只是需要输入程序的名字就可以了; 例如 kwrite

Note
如果你得到丢失mimetypes此类的错误, 试一试下面的方法:
  • run unset XDG_DATA_DIRS ; kbuildsycoca4
Note
{{{1}}}


使用SSH

The simplest way to run a KDE 4 application with SSH in your current desktop environment is to get an X-aware shell prompt as the kde-devel user like this: ssh -X kde-devel@localhost 现在可以按正常方法启动KDE应用了,例如: kwrite The two lines can be conveniently combined: ssh -X kde-devel@localhost kwrite

Note
如果碰到问题,参考上面的出错处理。


无密码登录

Before anything serious can be done using this method, a passwordless login needs to be set up. To start, run the following command as the regular desktop user: ssh-keygen -t rsa Hit enter three times to accept the path of ~/.ssh/id_rsa and an empty passphrase. Now, copy the single line in ~/.ssh/id_rsa.pub that's printed after running this command: cat ~/.ssh/id_rsa.pub After that line is copied, ssh back into the kde-devel user and put the copied line in the file $HOME/.ssh/authorized_keys:

ssh -X kde-devel@localhost $HOME/kde/bin/kwrite \
 $HOME/.ssh/authorized_keys

Paste in the line, save the file, and quit KWrite. Now try running KWrite again with the same SSH command; you shouldn't have to enter a password anymore: ssh -X kde-devel@localhost $HOME/kde/bin/kwrite

Warning
Using a passwordless SSH login has certain security risks, so make sure you protect your ~/.ssh/id_rsa file by restricting access to it with chmod og-xrw ~/.ssh/id_rsa(although the file should have these permissions when it is created)


SSH桌面文件

If you want to be able to launch apps more easily than running them with an SSH command from the command line, one way is to create .desktop files that ssh into the other account.

Note
This will only be useful if your desktop environment supports .desktop files, but at least KDE and GNOME do.


You can start with an existing .desktop file as a template (like one from your desktop) or you can make one from scratch. The main idea is to prefix the command being run with this string: ssh -X kde-devel@localhost $HOME/kde/bin/

一个运行KWrite的(.desktop)文件通常有下列内容:

[Desktop Entry] Categories=Qt;KDE;TextEditor; Comment= DocPath=kwrite/index.html Encoding=UTF-8 Exec=ssh -X kde-devel@localhost /home/kde-devel/kde/bin/kwrite %U GenericName=Text Editor Icon=kwrite InitialPreference=8 MimeType=text/plain Name=KWrite (kde-devel) Path= StartupNotify=false Terminal=false TerminalOptions= Type=Application X-DBUS-StartupType=Multi X-DCOP-ServiceType=non X-KDE-StartupNotify=true X-KDE-SubstituteUID=false X-KDE-Username=

Tip
Apps launched using SSH like this don't trigger the correct launch responses, so you probably want to disable "launch feedback" for your .desktop files


Note
In order to create a .desktop file for a KDE 4 app by using this pattern, the app's package will have to have been installed into ~/kde/bin using the cmakekde command


加载KDE 4会话

嵌套 KDE 4会话

Nested

Instead of using a full-blown new virtual X for developing software you can use Xephyr to embed your KDE 4 session into your working KDE 3 or other X11 environment.

You can also do this with xnest, but as xnest cannot handle extensions like Render many people prefer Xephyr.

If you want to get a minimal KDE session up and running, just launch Xephyr (available in Kubuntu as xserver-xephyr; Gentoo users compile x11-base/xorg-server with USE="kdrive"):

Xephyr :1&

加载KDE:

export DISPLAY=:1
/path/to/kde4/bin/startkde-modified &

startkde-modified is a copy of the startkde-script which includes the following lines on the top:

export KDEDIR=`kde4-config --prefix` export LD_LIBRARY_PATH=$KDEDIR/lib export PATH=$KDEDIR/bin/:$PATH export KDEHOME=~/.kde4

You can also use Xephyr with KDM via the Xdmcp protocol and simply a new KDE 4 session to KDM.

Kubuntu上, 可以这么做

[Xdmcp]

  1. Whether KDM should listen to incoming XDMCP requests.
  2. 缺省是 true

Enable=false

in /etc/kde3/kdm/kdmrc to

[Xdmcp]

  1. Whether KDM should listen to incoming XDMCP requests.
  2. 缺省是 true

Enable=true

and adjust your /etc/kde3/kdm/Xaccess to allow your local machine access. Additionally you should make sure to set up a port blocking policy on all external interfaces for the Xdmcp port if you are doing this on a laptop or a PC in an untrusted environment.

If you are done, simply launch Xephyr:

Xephyr -query localhost :1 -host-cursor -screen 1024x768&

where -host-cursor tries to reuse the host's cursor and -screen sets the screen dimensions.

Note: If you get lots of refused connection errors, you might want to use the -ac option of Xephyr. For example:

Xephyr -ac :1&

Another option to try if you get lots of refused connection errors is you may need to grant assess to your kde-devel user to your X server. As root or using sudo execute:

xhost +local:kde-devel

如果没有Xephyr,也可以用Xnest:

Xnest -ac :1& export DISPLAY=:1



Warning
This section needs improvements: Please help us to

cleanup confusing sections and fix sections which contain a todo


Sping 00:25, 9 April 2007 (CEST)
I use this for my start script nested_kde4.sh:

#! /bin/bash
NESTED_KDE_DISPLAY_BACKUP=$DISPLAY
export DISPLAY=:0
Xephyr :1 -screen 1024x768 &
export DISPLAY=:1
$HOME/kde/bin/startkde-modified &
export DISPLAY=${NESTED_KDE_DISPLAY_BACKUP}

If you run into

"Call to lnusertemp failed (temporary directories full?).
 Check your installation."

try this:

mkdir /var/tmp/kde-devel-kde4

The above code assumes you work with user kde-devel.

Note
In most cases you have to replace startkde-modified with startkde

Solitary KDE 4会话

Solitary

To run a full KDE 4 desktop environment session, you can either start it from the command line as you normally would, with something like this:

X :1 & export DISPLAY=:1
startkde
Note
If the X server refuses the connection saying something like: Xlib: connection to ":1.0" refused by server, try X -ac :1 instead.


or you can can add it to your login manager. If you are using KDM (or a compatible login manager) this is done by creating a .desktop file in either `kde-config --prefix`/share/apps/kdm/sessions/ or in /usr/share/xsessions/. The easiest thing to do is to copy an existing kde.desktop file and name it kde4.desktop. Open this new .desktop file in a text editor and change the Exec, TryExec and Name entries to look something like this:

Exec=$HOME/kde/bin/startkde TryExec=$HOME/kde/bin/startkde Name=KDE4

Replace $HOME/kde in the example above with the prefix you are installing KDE4 into.

After restarting the login manager (Alt+e in KDM) this new entry should appear in the sessions menu.

Note
You should have path to 'qdbus' program (usually it is $QTDIR/bin) in your $PATH to login successfully. If it is not there, you'll get an error message "Could not start DBus. Check your installation."


开发任务

KDevelop

This section will explain how to use KDevelop 3.4 to develop KDE 4 applications. If you have any questions, corrections or rants about this section, please post them on the discussion page.

前提条件

You need at least KDevelop 3.4 for this, which is still a KDE 3 application. Versions lower than 3.4 do not have Qt 4 support among other things. The KDE 4 version of KDevelop is not yet ready for serious development. You can get KDevelop at the KDevelop homepage. Make sure you install KDevelop like all other KDE 3 applications, not with your kde-devel user.

You also need the lastest GDB version, which is currently 6.6.0.

You need to have the kdelibs API documentation locally, which is described in the build instructions.

你还需要ctags, htdig, htmergehtsearch. valgrindcallgrind也是有用的。

Be sure you followed the steps in the KDE 4 build instructions and have a working KDE 4 environment. Make sure simple KDE 4 applications like Konsole or KWrite can be started from the command line of the kde-devel user without problems.

The following steps are all done with the kde-devel user. You need to login as that user by typing su - kde-devel.

设置环境

KDevelop并不与生俱来就支持CMake。但,CMake可以产生KDevelop项目文件,只要在执行cmake命令时加上 -GKDevelop3。他会告诉CMake,产生KDevelop项目文件和通用makefiles文件。The best way to do this is to modify your cmakekde function in your .bashrc。改变如下: cmake $srcFolder -DCMAKE_INSTALL_PREFIX=$KDEDIR \ -DCMAKE_BUILD_TYPE=debugfull&& \ make && \ make install; to cmake $srcFolder -GKDevelop3 -DCMAKE_INSTALL_PREFIX=$KDEDIR \ -DCMAKE_BUILD_TYPE=debugfull&& \ make && \ make install;

After you have done that, re-login so that the changes to the .bashrc file take effect. Then you need to rerun cmakekde in the (root) build directory of the project you want to work on with KDevelop (if you didn't use -GKDevelop3 on the building step). For example, if you want to work on Konsole, which lives in kdebase, you need to run cmakekde in the $KDE_BUILD/KDE/kdebase directory. This unfortunately completely rebuilds everything, but only once when you change the generator.

Since all environment variables of the kde-devel user are KDE 4 specific, these need to be set back to match your KDE 3 environment before starting KDevelop. A simple way to do this is to add the following function to your .bashrc:

function start3app {

 mkdir -p /tmp/$USER-kde
 export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games 
 export LD_LIBRARY_PATH= 
 export KDETMP=/tmp/$USER-kde 
 export KDEVARTMP=/var/tmp/$USER-kde 
 export KDEHOME=$HOME/.kde 
 export KDEDIR=/usr 
 export KDEDIRS=$KDEDIR 
 export DISPLAY=:0 
 eval "$@"
 source $HOME/.bashrc   #Reset environment variables again

}

The PATH and LD_LIBRARY_PATH variables are taken from the KDE 3 user, and they may be different on your system. Type echo $PATH and echo $LD_LIBRARY_PATH as normal KDE 3 user to get these values. The above function assumes that KDE 3 is installed in the /usr prefix, as it is the case on Debian-based systems. If your KDE 3 is installed to a different prefix, for example /opt/kde3, you need to change the line setting KDEDIR accordingly.

Now you should be able to start KDevelop by typing start3app kdevelop. Do that now.

Tip
You can start any KDE 3 application with the start3app function. Useful candidates include Kompare and kdesvn. However, you can not start KDbg this way to debug KDE 4 applications, since then the environment variables for the debugged application are wrong.


设置KDevelop

Now that KDevelop has started, you need to adjust a few settings. Go to Settings->Configure KDevelop...->Documentation for this. Remove all entries that are not relevant to KDE 4 coding.

Note
Although environment variables like $HOME are used in this section, you should replace them with real paths because KDevelop does not resolve environment variables.


Now click Add... to add the kdelibs API documentation. In this dialog, use the following settings:

  • Type: Doxygen Documentation Collection (needs to be set first)
  • Location: $KDE_SRC/KDE/kdelibs/kdelibs-apidocs/index.html

Now add the Qt API documentation, using the following settings:

  • Type: Qt Documentation Collection (needs to be set first)
  • Location: $HOME/qt-copy/doc/html/qt.dcf

After you have added kdelibs and Qt API documentation, make sure all checkboxes (TOC,Index and Search) are enabled. Then, go to the Full Text Search tab and make sure the paths to the htdig, htmerge and htsearch executables are correct. You can then close the settings dialog.

Now it is time to open the project you want to work on by clicking Project->Open Project.... The project files are located in the directory. For example, if you want to work on Konsole, you need to open $KDE_BUILD/KDE/kdebase/apps/konsole/konsole.kdevelop. You now need to adjust a few project-specific settings in Project->Project Options. You need to do this every time you start to work on a different project.

Note
Sometimes, a KDevelop project file is not present for the folder you want to work on.

This can have several reasons, it depends on how the CMake files are written. Usually, CMake files which have a project(projectname) statement in them should work fine. Once you are familiar enough with CMake, you can try adding the statement.

A workaround for this is to simply use the KDevelop project file of the parent folder, or even higher. In this case, you need to use the Make Active Directory entry in the context menu of the File Selector sidetab. With this, you can ignore the other unwanted folders when building and installing.


  • C++ Support->Code Completion
Here you need to add code completion databases for Qt and kdelibs, and more if you like, for example you might need a database for kdepimlibs when working on kdepim.
For kdelibs, click the Add... button and choose KDevelop Custom Directory PCS Importer, then add your KDE include directory ($HOME/kde/include) to the list and proceed. You can use the file selection dialog and the Add button to add it.
Now, add the database for Qt 4 by selecting KDevelop Qt4 PCS Importer this time. You need to select the Qt 4 include directory, which is $HOME/qt-copy/include.
Note
The Qt4 PCS Importer is only needed if you didn't install Qt4, i.e. you use it directly from the build directory. The drawback of using the Qt4 importer is that it doesn't show progress and the application seems to hang while it imports. The alternative is to use the Custom Directory PCS Importer for this too


  • C++ Support->Qt Options
Check Enable Qt options and choose Qt4 as your version. Set the QMake Binary path to $HOME/qt-copy/bin/qmake. Then choose Qt 4 style as Qt include syntax. Use $HOME/qt-copy/bin/designer as Designer Binary. Make sure to use the Change Plugin Paths dialog to add the plugin directory from KDE so you see the KDE widgets when designer is started. To do this add $HOME/kde/lib/kde4/plugins to the lineedit and then click the Add button.
  • Run Options
Make sure you use the correct binary as Executable. For example, if you want to run Konsole, this is $KDE_BUILD/KDE/kdebase/apps/konsole/src/konsole. You should add --nofork to the Debug Arguments or debugging some applications like KMail will not work at all.
Because the start3app functions changes some environment variables, you need to change them back here so the KDE 4 applications can be run without problems from within KDevelop.
For some applications, like Konsole, this is not strictly necessary, but others like KMail will crash if you do not change this.
Simply click the Add / Copy button to add new environment variables. You will need the following, which are the same as in your .bashrc:
Name Value
KDEHOME $HOME/.kde4
PATH $QTDIR/bin:$KDEDIR/bin:/usr/local/bin:$PATH
LD_LIBRARY_PATH $QTDIR/lib:$KDEDIR/lib:$LD_LIBRARY_PATH
KDETMP /tmp/$USER-kde4
KDEVARTMP /var/tmp/$USER-kde4
KDEDIR $HOME/kde
KDEDIRS $KDEDIR
LD_BIND_NOW 42


  • Build Options->Build
Make sure the correct build directory is selected. Again, for Konsole, this would be $KDE_BUILD/KDE/kdebase/apps/konsole.
  • Build Options->Make
You might want to check Abort on first error. You also might want to add VERBOSE= or VERBOSE=1 to Additional make options to control the level of verbosity for the build process.
If you have more than one processor or if you have access to an icecream cluster, you might want to check the Run multiple jobs option and set the Number of simultaneous jobs to the number of available processors. This increases the compile speed. It is the same as the -j option for make.
  • 格式(Formatting)
You should set all options here to match the coding style of the project you are working on.
  • CTags->General
You need to correctly set the Path to ctags binary, which is /usr/bin/ctags on Debian-based systems.
You probably want to enable the When more than one hit, go directly to the first option.

Now you have finished adjusting your project-specific settings. Now you should remove some plugins you do not need, in Settings->Configure Plugins.... I for example disable the following plugins:

Abbreviation Expansion, Code Snippets, Doxygen Support, Embedded Konsole, File Tree, Final Packaging Support, "Open with" Menu Addon, QuickOpen, Regular Expression Tester, Scripting, Security Checker, Shell Filtering and Insertion, Text Structure and Tools Menu Addition.

至少把那些粗体的给禁掉。

Now, open any source file if none is open already. This will enable the Settings->Configure Editor... entry, where you need to set the tab options to match the tab style used by the project you are working on. The important settings are:

  • Appearance->Borders->Show line numbers: 要检查。
  • Appearance->Borders->Show icon border: 要检查。
  • Editing->Tabulators
  • Editing->Static Word Wrap->Show marker: 要检查。
  • Indentation->Automatic Indentation->Indentation mode: 应该是C Style
  • Indentation 整体介绍

在主窗口, 点击下部的CTags插页, 然后选Regenerate按钮来创建一个CTags数据库;这样可以方便的在代码中穿梭来回。

至此,所有必要设置都完成了,恭喜,你可以开始工作了!

使用KDevelop

参考KDevelop手册,那里有关于KDevelop的整体介绍。下面会介绍些KDE 4的一些特殊事物。

调试

KDE有很多符号,这就意味着在调试时,你需要大内存。引用一个GDB开发员的话语: "在低于1GB内存的机器上调试KDE程序是件很讨厌的事情。" 如果调试的确很慢,试试下列技巧:

  • 隐藏本地变量。The Locals part of the variable tab on the left causes a big slowdown when stepping if you have many local variables. Simple collapse the Locals part of the tree, the local variables are then not updated every step. You can still examine variables by using the Evaluate expression function.
  • 用这个补丁:http://bugs.kde.org/show_bug.cgi?id=143977。It prevents the update of the framestack widget at each step, speeding up stepping considerably. The patch introduces some minor glitches, which is why it is not yet commited.
Note
KDevelop目前还不支持编辑CMake联编系统。This means you can not use KDevelop to add or remove files from the project or to change any other aspect of your project's build process. 你的手工编辑CMake文件and then rerun cmakekde instead. 请阅读 CMake教程以了解如何实施。


Tip
使用类库前,一定要先安装,然后你才能测试和调试。

这个很麻烦也很耗时间, 你应该对所有可能引用到的类库建立符号链接((ln -s)),把它们从构建目录连到安装目录。

通常,最简单的程序也会在其中调用一些类库,例如Konsole就是一种类库。