Blog Archive

Sunday, December 2, 2018

RPM 打包︰由一竅不通到動手濫用


(说明:本文是在原文的基础上修改错误而生成的)

Linux 世界除了 .tar 之外,有兩大主流打包格式,就是 .deb  .rpm,由於最近工作關係,學了一點點這兩個格式的打包技巧,希望和大家分享一下。
這篇文我們來談一下 .rpm 格式。
有興趣請跟着本文動手,打包你人生第一個 .rpm 檔吧!

簡介
rpm 1997 Erik Troan Marc Ewing 為紅帽 (RedHat Linux) 設計的包裝格式,設計參考一些更早的打包工具(pmpms),名字 rpm 的全寫是「RPM Package Manager」(對,是自我指涉的命名,總之別再問了)。
rpm 想解決的問題
我有一個 .tar 檔,有方法編譯,我應該怎樣指示包裝工具,同時產生源碼和二進制的裝包檔?
對,你只要給予適當的指示,rpm 工具可以同時支援打包源碼檔 SRPM (Source RPM) ,編譯程式,並裝編譯結果打包成 .rpm
如果你沒有打算打包源碼……其實還好,也有方法。
rpm 的解決方法
乾脆把你取得源碼、編譯、打包過程,通通用 bash 和巨集描述,其他的我們幫你。
rpm 打包的核心是 SPEC 設定檔,裏面供定義一些不同情況下執行的 bash 指令稿。
為了節省包裝者的工作,SPEC 支援用 %define 語句去定義變數 (variable) 和巨集 (macro)。除 rpm 官方提供一些內置巨集指令外,個別分發版本也提供一些指令集,支援 makefilepython 或者其他程式語言的打包工作。

動手打包
要動手打包前,首先得安裝相關的軟件。我們需要使用的程式是 rpmbuild
·       如果你是 RHELCentOS 或者 Fedora 的用戶
sudo yum install rpm-build 或者 sudo dnf install rpm-build
·       如果你是 Ubuntu 或者 Debian 的用戶, 
sudo apt-get install rpm
安裝之後,我們來試一試 rpmbuild是否存在︰
shell $  rpmbuild --version
RPM version 4.14.1
似乎可以了。
動手打包你第一個 rpm 吧!
我們得先有一個程式去打包,我們用 bash 指令稿做示範。首先用你愛用的Linux 文字編輯器,輸入下面的指令稿︰



#!/bin/bash # say something, with timestamp, every 5 secondswhile echo "$@"; do sleep 5sdone


存檔時請把它命名為 tick5 ,然後用 chmod u+x tick5來賦予執行權限,接着試用︰
./tick5 come on, James
結果應該像這樣(註︰看悶了請按 Ctrl+C 結束執行)︰
shell $  ./tick5 come on, James
come on, james
come on, james
come on, james
come on, james
come on, james
come on, james
...
...
接着我們在同一個資料匣,加入我們的 SPEC 檔︰


%define srcDir %(pwd) # Some metadata required by an RPM package Name: tick5
Summary: Print a message every 5s
Version: 0.1
Release: 1
License: MIT
%description
tick5 is a simple useless script that echos a message every 5 seconds.
%install
mkdir -p %{buildroot}/usr/bin
cp -pdf %{srcDir}/tick5 %{buildroot}/usr/bin/tick5
chmod 755 %{buildroot}/usr/bin/tick5

如果你上面都做得對,你的資料匣中會同時有這兩個檔案︰
tick5 tick5.spec
既然有了程式,又有了SPEC,我們就可以執行打包的指令︰
rpmbuild -bb tick5.spec
結果應該像這樣︰
shell $  rpmbuild -bb tick5.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.QZ1vBE
+ umask 022
+ cd /home/user/rpmbuild/BUILD
+ mkdir -p /home/user/rpmbuild/BUILDROOT/tick5-0.1-1.x86_64/usr/bin
+ cp -pdf /home/user/tick5/tick5 /home/user/rpmbuild/BUILDROOT/tick5-0.1-1.x86_64/usr/bin/tick5
+ chmod 755 /home/user/rpmbuild/BUILDROOT/tick5-0.1-1.x86_64/usr/bin/tick5
+ exit 0
Processing files: tick5-0.1-1.x86_64
Provides: tick5 = 0.1-1 tick5(x86-64) = 0.1-1
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires: /bin/bash
Checking for unpackaged file(s): /usr/lib/rpm/check-files /home/user/rpmbuild/BUILDROOT/tick5-0.1-1.x86_64
Wrote: /home/user/rpmbuild/RPMS/x86_64/tick5-0.1-1.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.XpUHeA
+ umask 022
+ cd /home/user/rpmbuild/BUILD
+ /bin/rm -rf /home/user/rpmbuild/BUILDROOT/tick5-0.1-1.x86_64
+ exit 0
完了啦?怎麼資料匣沒有新檔案的?我的 rpm 檔呢?留意上面的輸出有這樣一句︰
Wrote: /home/user/rpmbuild/RPMS/x86_64/tick5-0.1-1.x86_64.rpm
執行打包時, rpmbuild 會建立自己的「包裝工作間」 (Packaging Workspace),由內部變數 _topdir 定義,而預設的路徑為 $HOME/rpmbuild。你可以在指令加上 --define "_topdir $PWD/rpmbuild" 將路徑改為你目前的資料匣。
打包好了,試試安裝
如果你是 RHELCentOS 或者 Fedora 的用戶
sudo rpm -ivh /home/user/rpmbuild/RPMS/x86_64/tick5-0.1-1.x86.rpm
如果你是 Ubuntu 或者 Debian 的用戶,由於你的系統沒有 rpm 的相依性資料庫,無法知道你 rpm 所需的套件是否有正確安裝,用同一個指令會出現錯誤︰
rpm: RPM should not be used directly install RPM packages, use Alien instead!
rpm: However assuming you know what you are doing...
error: Failed dependencies:
 /bin/bash is needed by tick5-0.1-1.x86_64
如果你是
正確安裝方法是安裝 alien 工具,然後用 alien rpm 當成 .deb 安裝︰
sudo apt-get install alien
sudo alien -iv
/home/user/rpmbuild/RPMS/x86_64/tick5-0.1-1.x86.rpm
如果一切正常,你應該可以在系統找到 /usr/bin/tick5 這個無用的指令程式,而你可以這樣執行︰
shell $  tick5 come on, james
come on, james
come on, james
come on, james
come on, james
come on, james
come on, james
...
...

解除 tick5 程式安裝
安裝了沒有用的東西,當然想刪掉。
如果你是 RHELCentOS 或者 Fedora 的用戶
sudo rpm -e tick5
如果你是 Ubuntu 或者 Debian 的用戶︰
sudo apt-get remove tick5
這就把剛剛沒有營養的 tick5 給刪除掉了。

下一篇文章,會比較深入介紹上面其實發生了甚麼事,還有動手修改 SPEC 檔變成 systemd 服務。有興趣的朋友,敬請留意。
[总结]
如果你用:RedHat, CentOS, Fedora:
rpmbuild -bb tick5.spec
sudo rpm -ivh /home/user/rpmbuild/RPMS/x86_64/tick5-0.1-1.x86.rpm
tick5 come on, james
sudo rpm -e tick5



如果你用 Ubuntu, Debian
rpmbuild -bb tick5.spec
sudo apt-get install alien
sudo alien -iv /home/user/rpmbuild/RPMS/x86_64/tick5-0.1-1.x86.rpm 
tick5 come on, james
sudo apt-get remove tick5

=========================




Thursday, November 29, 2018

c/c++程序main函数参数解析,以及参数传递的不同方法




原文
1. 程序运行时传递参数的不同方法:

   1)通过命令参数传递参数; 2) 通过txt文件读取参数。

2. 方法举例

1)通过命令参数传递参数,即解析main函数的参数。

    main函数可以不带参数,也可以带参数,这个参数可以认为是 main函数的形式参数。C语言规定main函数的参数只能有两个,习惯上这两个参数写为argc和argv。所以C99标准中规定只有以下两种定义方式是正确的:
    int main(void) //无参形式
    int main(int argc, char *argv[]) //有参形式
当然有参形式可以进行演变,所以下面的写法也是正确的(同时变量名是可以更换的):
    int main(int argc, char **argv) //有参的另一种形式

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
int parseOptions(int argc, char** argv, int &imgWid, int &imgHei, int &imgCnt)
{
    if(argc == 1)
        return -1;
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-w") == 0)        {
         imgWid = (int)atoi(argv[++i]);
        }        else if (strcmp(argv[i], "-h") == 0)        {
         imgHei = (int)atoi(argv[++i]);
        }        else if (strcmp(argv[i], "-c") == 0)        {
            imgCnt = (int)atoi(argv[++i]);
        }        else
            return -1;
    }
 
    return 0;
}
 
int main(int argc, char** argv){ 
    int imgWid, imgHei, imgCnt; 
    if(-1 == parseOptions(argc, argv, imgWid, imgHei, imgCnt))    {
        printf("Get parameter failed!\n");
        exit(-1);
   } 
}
运行方法为:

  a. 在vs下运行时:在工程属性的命令参数行中输入:-w 640 -h 480 -c 10 即可
  b. 在DOS下如果生成可执行文件为example.exe放在D盘内,则输入:D:example -w 640 -h 480 -c 10 即可

2)通过TXT文件读取参数

int main(int argc, char** argv)
{
 
    int imgWid, imgHei, imgCnt;
 
    FILE* srcFile = NULL;
 
    srcFile = fopen("parameter_list.txt","r");
    if (srcFile == NULL)
   {
        printf("Open parameter_list failed!\n");
        exit(-1);
    }
    while (!feof(srcFile))
  {
      fscanf(srcFile,"%d", &imgWid);
      fscanf(srcFile,"%d", &imgHei);
      fscanf(srcFile,"%d", &imgCnt);
      printf("imgWid=%d imgHei=%d imgCnt=%d\n", imgWid, imgHei, imgCnt);
      ...
   }
   ...
}
parameter_list.txt文件中的参数如下:

640 480 10
1280 720 10
1920 1080 10
3840 2160 10


--------------------- 
作者:JonFred 
来源:CSDN 

版权声明:本文为博主原创文章,转载请附上博文链接!

Monday, November 26, 2018

6 Important lesson from Rich Dad,Poor Dad Book By Robert Kiyosaki

Here are the six major lessons discussed throughout the book:
  • Lesson #1 The Rich Don’t Work for Money
The rich dad says “Money works for me” – not the other way around. He wants you to work to let you learn how money works and how to have it work for you. But if you want to master how, first, you have to alleviate your fear.
According to the rich dad, fear is the primary emotion when the subject of money is discussed. It keeps most people to stay in their job because they fear of not being able to pay the bills or not having enough money. Greed is the second; if you offer more money, they also increase spending. If you don’t want to become slave to your money, you have to think about it logically, not through emotions.
  • Lesson #2 Why Teach Financial Literacy?
The rich dad believes that money without financial intelligence is money soon gone. This is why he stresses the importance of financial education. Schools only teach people how to work for money. They are designed to produce good employees instead of employers. What’s lacking in formal education is the so-called financial aptitude – what you do with the money once you make it, how to keep people from taking it from you, how long you keep it, and how hard that money works for you. If you want to become financially independent, you have to know and understand your numbers.
  • Lesson #3 Mind Your own Business
Your profession and your business are different from each other. You should not rely on your job to become financially secure; instead, focus on your own business and develop your assets. As Kiyosaki puts it, “financial struggle is often directly the result of people working all their life for someone else. Many people will have nothing at the end of their working days.” Acquire assets and use them to purchase income-generating, real assets. The rich dad further noted that real assets are anything with value – stocks, bonds, mutual funds, income-producing real estate, notes, royalties from intellectual property, etc.
  • Lesson #4 The History of Taxes and the Power of Corporations
The idea of taxes started when people were made believe that it was intended to punish the rich. In reality, the poor and middle class, the very people who accepted and voted for it, were the ones disadvantaged. “Once government got a taste of money, the appetite grew,” said the rich dad. In the government, the larger his organization, the more he is respected. On the other hand, within my organization, the fewer people I hire and the less money I spend, the more I am respected by my investors.
The point is, the rich people are aware of how much they are being manipulated by this system, but they don’t let these hinder them. Having a good knowledge in four broad areas of expertise – accounting, investing, understanding markets, and law – make them understand how to protect and enhance their assets.
  • Lesson #5 The Rich Invent Money
The one thing that holds all of us back is self-doubt. While most people wait around for luck and opportunity, the rich ones are creating opportunities out of obstacles. In the real world, it’s not the smart that gets ahead but the bold. If you don’t think big and take risks, you will spend your entire life waiting for something that is not bound to happen.
Kiyosaki reiterated here the importance of financial education. The rich doesn’t think that he can make more money just by working harder; he also strives to become financially literate to explore more options and have more power.
  • Lesson #6 Work to Learn Don’t Work for Money
Kiyosaki thinks that there are people who are “one skill away from great wealth.” Aside from the things we learn in school, we also need to learn the skills needed to achieve financial success. The rich people seek work for what they will learn, more than what they will earn.
Some of the significant areas to focus on are management and communication. Kiyosaki also stressed the importance of sales and marketing skills. These are specific knowledge that people should acquire to help them reach the road towards financial freedom.
Rich people perceive money differently. They know that it is not enough to make money – you also have to grow it and learn how to manage it. It’s one thing to become financially independent, and another to become financially literate. Having knowledge when it comes to money matters is essential not just for investors, but for everyone. The financial decisions you face on a daily basis will have a major impact in your future so you should know how to be responsible for these.