前情提要
長久以來寫C/C++的Code時,配置環境是很麻煩的一件事,通常會遇到以下的事情
- 下載第三方Library
- 設定Include Path
- 設定Linking library
- Cross Platform的設置
- Makefile的編寫
雖然有了CMake幫助解決塊平台的問題。不過還是不夠。有時我們想要Rust的Cargo或是Go Build這樣的東西,而Biicode給了我們一點光明。
而這邊就是將教學和Building C / C++ applications with biicode用自己的方式表達出來。
Create a project
1 2 3
| $ bii init myproject $ cd myproject && ls bii blocks deps
|
biicode幫我們產生了三個目錄
接著產生一個Hello World的Template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ bii new myuser/crypto --hello=cpp $ bii cpp:build INFO: Processing changes... Running: cmake -G "Unix Makefiles" -Wno-dev ../cmake
BLOCK: myuser/crypto ----------------------------------------------------------- + LIB: myuser_crypto + EXE: myuser_crypto_main -- Configuring done -- Generating done -- Build files have been written to: /home/hm/build/myproject/build Building: cmake --build . Scanning dependencies of target myuser_crypto_main [100%] Building CXX object myuser/crypto/CMakeFiles/myuser_crypto_main.dir/main.cpp.o Linking CXX executable /home/hm/build/myproject/bin/myuser_crypto_main [100%] Built target myuser_crypto_main
|
可以看到最後產生的Program放在bin/
目錄底下,於是我們可以執行
1 2
| $ ./bin/myuser_crypto_main Hello world!
|
到這邊沒什麼特別的,有趣的事情在後面
Dependency management
修改blocks/myuser/crypto/main.cp
成如下,看Code我們可以猜到他需要Crypto++
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include "cryptopp/cryptopp/sha.h" #include "cryptopp/cryptopp/filters.h" #include "cryptopp/cryptopp/hex.h" #include <iostream> #include <string>
int main() { CryptoPP::SHA1 sha1; std::string source = "Hello"; std::string hash = ""; CryptoPP::StringSource(source, true, new CryptoPP::HashFilter(sha1, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash)))); std::cout << hash << std::endl; }
|
接著重新Build一次
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $ bii cpp:build INFO: Processing changes... Running: cmake -G "Unix Makefiles" -Wno-dev ../cmake
BLOCK: myuser/crypto ----------------------------------------------------------- + LIB: myuser_crypto + EXE: myuser_crypto_main -- Configuring done -- Generating done -- Build files have been written to: /home/hm/build/myproject/build Building: cmake --build . Scanning dependencies of target myuser_crypto_main [100%] Building CXX object myuser/crypto/CMakeFiles/myuser_crypto_main.dir/main.cpp.o /home/hm/build/myproject/blocks/myuser/crypto/main.cpp:1:35: fatal error: cryptopp/cryptopp/sha.h: No such file or directory #include "cryptopp/cryptopp/sha.h" ^ compilation terminated. make[2]: *** [myuser/crypto/CMakeFiles/myuser_crypto_main.dir/main.cpp.o] Error 1 make[1]: *** [myuser/crypto/CMakeFiles/myuser_crypto_main.dir/all] Error 2 make: *** [all] Error 2 ERROR: Build failed
|
正如所料,找不到對應的Header跟Library。此時我們可以使用
1 2 3 4 5 6 7 8 9 10 11 12
| $ bii find INFO: Processing changes... INFO: Finding missing dependencies in server INFO: Looking for cryptopp/cryptopp... INFO: >> Block candidate: cryptopp/cryptopp/cryptopp/master ....(略) INFO: Analyzing compatibility for found dependencies... INFO: All dependencies resolved Find resolved new dependencies: cryptopp/cryptopp: 8 INFO: Downloading files from: cryptopp/cryptopp INFO: Saving files from: cryptopp/cryptopp
|
接著在一次編譯,就會得到我們想要的結果
1 2 3
| $ bii cpp:build $ ./bin/myuser_crypto_main F7FF9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0
|
如果我們使用bii deps
,可以到看我們需要的相依性
1 2 3 4 5 6 7 8 9
| INFO: Processing changes... myuser/crypto depends on: cryptopp/cryptopp: 8 filters.h hex.h sha.h system: iostream string
|
感想
雖然這看起來很酷,不過還是有幾點希望改進
- Cross Compiler Toolchain
- Android NDK and etc
- More Library support
真期待這東西更加成熟啊