我是在windows xp-sp2上使用SSHSecureShellClient-3[1].2.9.exe進行linux C編程的,下面先講一下怎麼使用這個軟件:
在本地安裝SSHSecureShellClient-3[1].2.9.exe,在服務器上建立用戶名和密碼,然後打開SSH Secure Shell Client,enter鍵或者點擊quick connect,輸入目標機(我不懂,瞎叫)ip,輸入用戶名,enter鍵,輸入密碼。現在你應該已經進入linux了,在這裡所有的linux命令都可以使用,新手不妨先敲幾個常用的命令試一下!
下面從最簡單的hello word講到比較復雜的庫引用,當然怎麼寫通用的makefile限於水平,不做討論。
1、hello word
1)、程序:
#include <stdio.h>
int main(void)
{
printf ("hello, word\n");
return 0;
}
名字為helloword.c
2)、運行:
進入SSH Secure Shell Client,使用 《cd 路徑名》進入到你的helloword.c所在的目錄,
然後gcc -o helloword helloword.c,下面將顯示編譯的情況,有錯誤和警告將會列出,沒有則不顯示而返回的你剛才進的目錄,要運行則輸入./helloword, 這樣就會看到hello, word
2、編譯多個.c和.h文件組成的程序,這時需要寫makefile
1)、程序:
#include "mytool1.h"
#include "mytool2.h"
int main(int argc,char **argv)
{
mytool1_print("hello");
mytool2_print("hello");
}
名字為main.c
#include "mytool1.h"
void mytool1_print(char *print_str)
{
printf("This is mytool1 print %s\n",print_str);
}
名字為mytool1.c
#ifndef _MYTOOL_1_H
#define _MYTOOL_1_H
void mytool1_print(char *print_str);
#endif
名字為mytool1.h
#include "mytool2.h"
void mytool2_print(char *print_str)
{
printf("This is mytool2 print %s\n",print_str);
}
名字為mytool2.c
#ifndef _MYTOOL_2_H
#define _MYTOOL_2_H
void mytool2_print(char *print_str);
#endif
名字為mytool2.h
2)、makefile
main:main.o mytool1.o mytool2.o
gcc -o main main.o mytool1.o mytool2.o
main.o:main.c mytool1.h mytool2.h
gcc -c main.c
mytool1.o:mytool1.c mytool1.h
gcc -c mytool1.c
mytool2.o:mytool2.c mytool2.h
gcc -c mytool2.c
3)/運行
[root@svn helloword]# cd -
/home/mengj
[root@svn mengj]# cd makefileexample
[root@svn makefileexample]# make
cc -c -o main.o main.c
cc -c -o mytool1.o mytool1.c
mytool1.c: In function 鈓ytool1_print?
mytool1.c:4: warning: incompatible implicit declaration of built-in function 鈖rintf?
cc -c -o mytool2.o mytool2.c
mytool2.c: In function 鈓ytool2_print?
mytool2.c:4: warning: incompatible implicit declaration of built-in function 鈖rintf?
gcc -o main main.o mytool1.o mytool2.o
main.o:main.c mytool1.h mytool2.h
make: main.o:main.c: Command not found
make: *** [main] Error 127
[root@svn makefileexample]# ./main
This is mytool1 print hello
This is mytool2 print hello
[root@svn makefileexample]#
上面就是這個程序的運行過程
先進入目錄,然後make,編譯通過運行./make
3、需要連接庫的程序,這個過程相對復雜
1)、程序
*
* gjobread.c : a small test program for gnome jobs XML format
*
* See Copyright for the status of this software.
*
* [email protected]
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* This example should compile and run indifferently with libxml-1.8.8 +
* and libxml2-2.1.0 +
* Check the COMPAT comments below
*/
/*
* COMPAT using xml-config --cflags to get the include path this will
* work with both
*/
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#define DEBUG(x) printf(x)
/*
* A person record
* an xmlChar * is really an UTF8 encoded char string (0 terminated)
*/
typedef struct person {
xmlChar *name;
xmlChar *email;
xmlChar *company;
xmlChar *organisation;
xmlChar *smail;
xmlChar *webPage;
xmlChar *phone;
} person, *personPtr;
/*
* And the code needed to parse it
*/
static personPtr
parsePerson(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
personPtr ret = NULL;
DEBUG("parsePerson\n");
/*
* allocate the struct
*/
ret = (personPtr) malloc(sizeof(person));
if (ret == NULL) {
fprintf(stderr,"out of memory\n");
return(NULL);
}
memset(ret, 0, sizeof(person));
/* We don't care what the top level element name is */
/* COMPAT xmlChildrenNode is a macro unifying libxml1 and libxml2 names */
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"Person")) &&
(cur->ns == ns))
ret->name = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if ((!xmlStrcmp(cur->name, (const xmlChar *)"Email")) &&
(cur->ns == ns))
ret->email = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
cur = cur->next;
}
return(ret);
}
/*
* and to print it
*/
static void
printPerson(personPtr cur) {
if (cur == NULL) return;
printf("------ Person\n");
if (cur->name) printf(" name: %s\n", cur->name);
if (cur->email) printf(" email: %s\n", cur->email);
if (cur->company) printf(" company: %s\n", cur->company);
if (cur->organisation) printf(" organisation: %s\n", cur->organisation);
if (cur->smail) printf(" smail: %s\n", cur->smail);
if (cur->webPage) printf(" Web: %s\n", cur->webPage);
if (cur->phone) printf(" phone: %s\n", cur->phone);
printf("------\n");
}
/*
* a Description for a Job
*/
typedef struct job {
xmlChar *projectID;
xmlChar *application;
xmlChar *category;
personPtr contact;
int nbDevelopers;
personPtr developers[100]; /* using dynamic alloc is left as an exercise */
} job, *jobPtr;
/*
* And the code needed to parse it
*/
static jobPtr
parseJob(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur) {
jobPtr ret = NULL;
DEBUG("parseJob\n");
/*
* allocate the struct
*/
ret = (jobPtr) malloc(sizeof(job));
if (ret == NULL) {
fprintf(stderr,"out of memory\n");
return(NULL);
}
memset(ret, 0, sizeof(job));
/* We don't care what the top level element name is */
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *) "Project")) &&
(cur->ns == ns)) {
ret->projectID = xmlGetProp(cur, (const xmlChar *) "ID");
printf("ret->projectID: %s\n", ret->projectID);
if (ret->projectID == NULL) {
fprintf(stderr, "Project has no ID\n");
}
}
if ((!xmlStrcmp(cur->name, (const xmlChar *) "Application")) &&
(cur->ns == ns))
ret->application =
xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if ((!xmlStrcmp(cur->name, (const xmlChar *) "Category")) &&
(cur->ns == ns))
ret->category =
xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if ((!xmlStrcmp(cur->name, (const xmlChar *) "Contact")) &&
(cur->ns == ns))
ret->contact = parsePerson(doc, ns, cur);
cur = cur->next;
}
return(ret);
}
/*
* and to print it
*/
static void
printJob(jobPtr cur) {
int i;
if (cur == NULL) return;
printf("======= Job\n");
if (cur->projectID != NULL) printf("projectID: %s\n", cur->projectID);
if (cur->application != NULL) printf("application: %s\n", cur->application);
if (cur->category != NULL) printf("category: %s\n", cur->category);
if (cur->contact != NULL) printPerson(cur->contact);
printf("%d developers\n", cur->nbDevelopers);
for (i = 0;i < cur->nbDevelopers;i++) printPerson(cur->developers[i]);
printf("======= \n");
}
/*
* A pool of Gnome Jobs
*/
typedef struct gjob {
int nbJobs;
jobPtr jobs[500]; /* using dynamic alloc is left as an exercise */
} gJob, *gJobPtr;
static gJobPtr
parseGjobFile(char *filename) {
xmlDocPtr doc;
gJobPtr ret;
jobPtr curjob;
xmlNsPtr ns;
xmlNodePtr cur;
/*
* build an XML tree from a the file;
*/
doc = xmlParseFile(filename);
if (doc == NULL) return(NULL);
/*
* Check the document is of the right kind
*/
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return(NULL);
}
ns = xmlSearchNsByHref(doc, cur,
(const xmlChar *) "http://www.gnome.org/some-location");
if (ns == NULL) {
fprintf(stderr,
"document of the wrong type, GJob Namespace not found\n");
xmlFreeDoc(doc);
return(NULL);
}
if (xmlStrcmp(cur->name, (const xmlChar *) "Helping")) {
fprintf(stderr,"document of the wrong type, root node != Helping");
xmlFreeDoc(doc);
return(NULL);
}
printf("cur->name: %s\n", cur->name);
/*
* Allocate the structure to be returned.
*/
ret = (gJobPtr) malloc(sizeof(gJob));
printf("sizeof(gJob): %d\n", sizeof(gJob));
if (ret == NULL) {
fprintf(stderr,"out of memory\n");
xmlFreeDoc(doc);
return(NULL);
}
memset(ret, 0, sizeof(gJob));
/*
* Now, walk the tree.
*/
/* First level we expect just Jobs */
cur = cur->xmlChildrenNode;
while ( cur && xmlIsBlankNode ( cur ) )
{
cur = cur -> next;
}
if ( cur == 0 )
return ( NULL );
if ((xmlStrcmp(cur->name, (const xmlChar *) "Jobs")) || (cur->ns != ns)) {
fprintf(stderr,"document of the wrong type, was '%s', Jobs expected",
cur->name);
fprintf(stderr,"xmlDocDump follows\n");
xmlDocDump ( stderr, doc );
fprintf(stderr,"xmlDocDump finished\n");
xmlFreeDoc(doc);
free(ret);
return(NULL);
}
printf("cur->name: %s\n", cur->name);
printf("cur->ns: %d\n", cur->ns);
/* Second level is a list of Job, but be laxist */
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *) "Job")) &&
(cur->ns == ns)) {
curjob = parseJob(doc, ns, cur);
if (curjob != NULL)
ret->jobs[ret->nbJobs++] = curjob;
if (ret->nbJobs >= 500) break;
}
cur = cur->next;
}
return(ret);
}
static void
handleGjob(gJobPtr cur) {
int i;
/*
* Do whatever you want and free the structure.
*/
printf("%d Jobs registered\n", cur->nbJobs);
for (i = 0; i < cur->nbJobs; i++) printJob(cur->jobs[i]);
}
int main(int argc, char **argv) {
printf("test start!\n");
int i;
gJobPtr cur;
/* COMPAT: Do not genrate nodes for formatting spaces */
LIBXML_TEST_VERSION
xmlKeepBlanksDefault(0);
for (i = 1; i < argc ; i++) {
cur = parseGjobFile(argv[i]);
if ( cur )
handleGjob(cur);
else
fprintf( stderr, "Error parsing file '%s'\n", argv[i]);
}
/* Clean up everything else before quitting. */
xmlCleanupParser();
printf("test end!\n");
return(0);
}
2)、鏈接庫#include <libxml/xmlmemory.h> #include <libxml/parser.h>
libxml所在的目錄為/home/mengj/libxml/inlcude 這個目錄裡安裝了libxml2
安裝的過程是在網上下載然後
(a) ./configure
(b) make
(c)make install
3)、運行
[root@svn example1]# gcc -o 1 1.c -L/home/mengj/libxml/include -lxml2 -I/home/mengj/libxml/include
[root@svn example1]# ./1 1.xml
test start!
cur->name: Helping
sizeof(gJob): 2004
cur->name: Jobs
cur->ns: 157174584
parseJob
ret->projectID: 12
parsePerson
1 Jobs registered
======= Job
projectID: 12
application: GBackup
category: Development
------ Person
name: Nathan Clemons
email: [email protected]
------
0 developers
=======
test end!
[root@svn example1]#
-L/home/mengj/libxml/include -lxml2 -I/home/mengj/libxml/include
表示需要連接的庫所在的路徑
ok, 到這裡我已經花了兩天時間熟悉,如果寫的足夠簡潔希望以後的新手能在一個上午搞定!