我正嘗試在C++中創建並鏈接一個靜態庫。我有兩個文件:
helloWorld.cppG ++/C++無法鏈接庫
#include <iostream>
int main(int argc, char const *argv[]) {
std::cout << "I'm inside main." << '\n';
printHello();
return 0;
}
libraryHello.cpp
#include <iostream>
void printHello() {
std::cout << "Hello" << '\n';
}
我運行這些命令(基於http://www.techytalk.info/c-cplusplus-library-programming-on-linux-part-one-static-libraries/):
前兩位順利,當試圖編譯主文件時出現這樣的錯誤:
helloWorld.cpp: In function ‘int main(int, const char**)’:
helloWorld.cpp:5:14: error: ‘printHello’ was not declared in this scope
它看起來好像根本沒有加載它並且找不到printHello。編譯,鏈接或其他任何事情時,我有什麼錯誤嗎?我想要做的就是使用靜態庫從main調用printHello()過程並將其鏈接到helloWorld.cpp。
我的編譯器:G ++ 5.4.0,操作系統:Ubuntu的16.04 32位