Homebrew進階使用教程(三)-apue.h在mac下安裝並使用鏈接

【homebrew 系列文章】html

  1. HomeBrew常規使用教程
  2. Homebrew進階使用教程(一)
  3. Homebrew進階使用教程(二)-用一個命令行天氣客戶端構建本身的倉庫
  4. Homebrew進階使用教程(三)-apue.h在mac下安裝並使用鏈接

個人github地址:github地址:https://github.com/rangaofei/homebrew-sakagit

上篇文章講了如何建立本身的倉庫,此次簡單講解如何上傳本身的庫或者程序到本身的倉庫。最近正在學習apue這本書,可是須要依賴做者的一個apue庫,本篇將以unix環境高級編程的庫apue.h和libapue.a文件的關聯講解如何上傳庫到本身的倉庫而後用brew安裝github

1. 建立安裝腳本

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

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

tar -cvzf apue-1.0.tar.gz ./*  
複製代碼

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

執行命令bash

brew create <url>
複製代碼

便可建立咱們的安裝腳本,腳本名稱默認爲前邊提到的庫名稱,對應個人命令爲ide

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

生成的文件爲apue.rb函數

2. 改寫安裝腳本

執行上邊的命令後會自動打開安裝腳本爲可編輯狀態,此處個人電腦自動使用vim打開,並生成一系列的代碼:post

# 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,現學現賣)。

class即爲咱們的目標庫。 裏邊比較重要的字段就是列出來的幾個desc,homepage,url,sha256

url和sha256是建立時自動生成的,url是下載地址,sha256是驗證碼,假如不匹配則會中止安裝,因此此處必定要填寫正確。 desc是描述字段,對庫的簡介,homepage按官方的說法是這個庫的官方網址。

3. 修改安裝方式

install函數是安裝時執行的動做,默認提供了make安裝和cmake(註釋部分)的安裝方式。此次咱們不用兩個方式,而是採用直接複製文件到目標文件夾的方式。

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是一個前綴,這個前綴帶包一系列的目標文件夾。

前綴表明的文件夾

前綴名稱 目標文件夾 示例
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文件複製到/usr/local/Celler/apue/lib文件夾下,同理include.iinstall apue.h將會將apue.h文件複製到/usr/local/Celler/apue/include文件夾下。

4. 發佈到倉庫

剛纔咱們編寫的apue.rb文件在哪裏呢? brew默認會在core`倉庫中建立這個文件,執行以下命令

cd $(brew --repo homebrew/core)
cd Formula/
ls |grep apue
複製代碼

在這裏便可看到輸出的文件中有咱們剛纔編輯的apue.rb。copy到本身的倉庫文件夾下並加入git管理推送到遠程倉庫,此時其餘人只要tap了這個倉庫就能夠安裝這個庫了。

mv apue.rb ../../../rangaofei/homebrew-saka/Formula #移動文件到本身的倉庫
cd $(brew --repo rangaofei/saka) #打開本身的倉庫
cd Formula  #進入文件夾
git add --all #加入git管理
git commit -m 'add new formula apue' #提交
git push #遠程倉庫提交
複製代碼

而後看一下咱們的庫的信息

經過brew install apue來安裝庫,安裝成功後,來看一下有沒有生成對應的文件

能夠看到在/usr/local/libusr/local/include文件夾下已經生成了兩個軟鏈接,鏈接到咱們的brew安裝目錄。這些工做都是homebrew自動執行的,當執行brew uninstall apue後這些軟鏈接也會自動刪除。這樣咱們的庫就發佈完成了。

5. 驗證使用

咱們已經安裝好了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。 而後執行外部構建,進入工程目錄

mkdir build
cd build/
cmake ..
make
複製代碼

構建過程當中沒有發生錯誤,執行文件可正確輸出。

至此驗證玩意

相關文章
相關標籤/搜索