Homebrew進(jìn)階使用教程(三)-apue.h在mac下安裝并使用連接

【homebrew 系列文章】

  1. HomeBrew常規(guī)使用教程
  2. Homebrew進(jìn)階使用教程(一)
  3. Homebrew進(jìn)階使用教程(二)-用一個命令行天氣客戶端構(gòu)建自己的倉庫
  4. Homebrew進(jìn)階使用教程(三)-apue.h在mac下安裝并使用連接

我的github地址:github地址:https://github.com/rangaofei/homebrew-saka

上篇文章講了如何創(chuàng)建自己的倉庫,這次簡單講解如何上傳自己的庫或者程序到自己的倉庫。最近正在學(xué)習(xí)apue這本書,但是需要依賴作者的一個apue庫,本篇將以unix環(huán)境高級編程的庫apue.h和libapue.a文件的關(guān)聯(lián)講解如何上傳庫到自己的倉庫然后用brew安裝

1. 創(chuàng)建安裝腳本

首先把自己的文件壓縮為tar.gz(官方推薦為這種壓縮格式,其他的沒有嘗試)格式文件,名稱以"庫名稱-版本號"格式書寫,這樣便于homebrew識別自己的版本號自動填寫。

此處我已下載好apue的源碼并且編譯好,我們只需要兩個文件include文件夾下的apue.h文件和lib文件夾下的libapue.a文件,將這兩個文件隨便拷貝到一個文件夾下,只包含
apue.hlibapue.a,命令行進(jìn)入這個文件夾并將這兩個文件打包:

tar -cvzf apue-1.0.tar.gz ./*  

此時會自動生成這個文件,將這個文件上傳到某個所有人能訪問的地址,此處我上傳到了https://raw.githubusercontent.com/rangaofei/apue/master/lib/apue-1.0.tar.gz這個地址。

執(zhí)行命令

brew create <url>

即可創(chuàng)建我們的安裝腳本,腳本名稱默認(rèn)為前邊提到的庫名稱,對應(yīng)我的命令為

brew create https://raw.githubusercontent.com/rangaofei/apue/master/lib/apue-1.0.tar.gz

生成的文件為apue.rb。

2. 改寫安裝腳本

執(zhí)行上邊的命令后會自動打開安裝腳本為可編輯狀態(tài),此處我的電腦自動使用vim打開,并生成一系列的代碼:

# Documentation: https://docs.brew.sh/Formula-Cookbook.html
#                http://www.rubydoc.info/github/Homebrew/brew/master/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
 class Apue < Formula
  desc ""
  homepage ""
  url "https://raw.githubusercontent.com/rangaofei/apue/master/lib/apue-1.0.tar.gz"
  sha256 "7e84d03563f7f0119f2d946cc9f439192e582b65a504c39f4158fea7f38f7cbd"
   def install
    # ENV.deparallelize
    system "./configure", "--disable-debug",
                          "--disable-dependency-tracking",
                          "--disable-silent-rules",
                          "--prefix=#{prefix}"
    # system "cmake", ".", *std_cmake_args
    system "make", "install"
  end

   test do
    # `test do` will create, run in and delete a temporary directory.
    #
    # This test will fail and we won't accept that! For Homebrew/homebrew-core
    # this will need to be a test that verifies the functionality of the
    # software. Run the test with `brew test apue`. Options passed
    # to `brew install` such as `--HEAD` also need to be provided to `brew test`.
    #
    # The installed folder is not in the path, so use the entire path to any
    # executables being tested: `system "#{bin}/program", "do", "something"`.
    system "false"
  end
end

遵循ruby語法(我完全不懂ruby,現(xiàn)學(xué)現(xiàn)賣)。

class即為我們的目標(biāo)庫。
里邊比較重要的字段就是列出來的幾個desc,homepage,url,sha256。

url和sha256是創(chuàng)建時自動生成的,url是下載地址,sha256是驗證碼,假如不匹配則會停止安裝,所以此處一定要填寫正確。
desc是描述字段,對庫的簡介,homepage按官方的說法是這個庫的官方網(wǎng)址。

3. 修改安裝方式

install函數(shù)是安裝時執(zhí)行的動作,默認(rèn)提供了make安裝和cmake(注釋部分)的安裝方式。這次我們不用兩個方式,而是采用直接復(fù)制文件到目標(biāo)文件夾的方式。

def install
     lib.install "libapue.a"
     include.install "apue.h"
end

注意此處的lib和include這兩個字段,和cmake中的install基本雷同。
看一下官方的說明:

prefix.install "file1", "file2" #安裝部分文件
prefix.install Dir["output/*"] #安裝整個output文件夾下的所有文件

prefix是一個前綴,這個前綴帶包一系列的目標(biāo)文件夾。

前綴代表的文件夾

前綴名稱 目標(biāo)文件夾 示例
HOMEBREW_PREFIX /usr/local
prefix #{HOMEBREW_PREFIX}/Cellar/#{name}/#{version} /usr/local/Cellar/foo/0.1
opt_prefix #{HOMEBREW_PREFIX}/opt/#{name} /usr/local/opt/foo
bin #{prefix}/bin /usr/local/Cellar/foo/0.1/bin
doc #{prefix}/share/doc/foo /usr/local/Cellar/foo/0.1/share/doc/foo
include #{prefix}/include /usr/local/Cellar/foo/0.1/include
info #{prefix}/share/info /usr/local/Cellar/foo/0.1/share/info
lib #{prefix}/lib /usr/local/Cellar/foo/0.1/lib
libexec #{prefix}/libexec /usr/local/Cellar/foo/0.1/libexec
man #{prefix}/share/man /usr/local/Cellar/foo/0.1/share/man
man[1-8] #{prefix}/share/man/man[1-8] /usr/local/Cellar/foo/0.1/share/man/man[1-8]
sbin #{prefix}/sbin /usr/local/Cellar/foo/0.1/sbin
share #{prefix}/share /usr/local/Cellar/foo/0.1/share
pkgshare #{prefix}/share/foo /usr/local/Cellar/foo/0.1/share/foo
etc #{HOMEBREW_PREFIX}/etc /usr/local/etc
var #{HOMEBREW_PREFIX}/var /usr/local/var
buildpath A temporary directory somewhere on your system /private/tmp/[formula-name]-0q2b/[formula-name]

解釋一下:

lib.install libapue.a將會將libapue.a文件復(fù)制到/usr/local/Celler/apue/lib文件夾下,同理include.iinstall apue.h將會將apue.h文件復(fù)制到/usr/local/Celler/apue/include文件夾下。

4. 發(fā)布到倉庫

剛才我們編寫的apue.rb文件在哪里呢?
brew默認(rèn)會在core`倉庫中創(chuàng)建這個文件,執(zhí)行如下命令

cd $(brew --repo homebrew/core)
cd Formula/
ls |grep apue

在這里即可看到輸出的文件中有我們剛才編輯的apue.rb。copy到自己的倉庫文件夾下并加入git管理推送到遠(yuǎn)程倉庫,此時其他人只要tap了這個倉庫就可以安裝這個庫了。

mv apue.rb ../../../rangaofei/homebrew-saka/Formula #移動文件到自己的倉庫
cd $(brew --repo rangaofei/saka) #打開自己的倉庫
cd Formula  #進(jìn)入文件夾
git add --all #加入git管理
git commit -m 'add new formula apue' #提交
git push #遠(yuǎn)程倉庫提交

然后看一下我們的庫的信息


1.png

通過brew install apue來安裝庫,安裝成功后,來看一下有沒有生成對應(yīng)的文件

2.png

3.png

可以看到在/usr/local/libusr/local/include文件夾下已經(jīng)生成了兩個軟連接,連接到我們的brew安裝目錄。這些工作都是homebrew自動執(zhí)行的,當(dāng)執(zhí)行brew uninstall apue后這些軟連接也會自動刪除。這樣我們的庫就發(fā)布完成了。

5. 驗證使用

我們已經(jīng)安裝好了apue這個庫,那我們就可以立即使用了。這里我用clion編寫了第一個示例代碼:

#include "apue.h"
#include <dirent.h>

int main(int argc, char *argv[]) {
    DIR *dp;
    struct dirent *dirp;

    if (argc != 2) {
        err_quit("usage:ls directory_name");
    }

    if ((dp = opendir(argv[1])) == NULL) {
        err_sys("can't open %s", argv[1]);
    }
    while ((dirp = readdir(dp)) != NULL) {
        printf("%s\n", dirp->d_name);
    }
    closedir(dp);
    exit(0);
}

第一行的頭文件就是剛才我們安裝的庫,此處是可以正確引用的,然后修改cmakelists文件

cmake_minimum_required(VERSION 3.9)
project(apue C)

set(CMAKE_C_STANDARD 99)

add_executable(apue main.c)

target_link_libraries(apue apue.a)

在最后一行為我們的庫添加了連接庫apue.a。
然后執(zhí)行外部構(gòu)建,進(jìn)入工程目錄

mkdir build
cd build/
cmake ..
make

構(gòu)建過程中沒有發(fā)生錯誤,執(zhí)行文件可正確輸出。

至此驗證玩意

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容