簡體   English   中英

gtest - 對 `testing::InitGoogleTest(int*, char**)' 的未定義引用

[英]gtest - undefined reference to `testing::InitGoogleTest(int*, char**)'

我正在嘗試創建 makefile 並編譯gtest簡單示例,但出現錯誤:

g++ main.o -o exampleOutput main.o: 在函數main': main.cpp:(.text+0x1e): undefined reference to testing::InitGoogleTest(int*, char**)' collect2: 錯誤:ld 返回 1退出狀態使:*** [輸出] 錯誤 1

這是main.cpp:

#include <iostream>
#include "gtest/gtest.h"

using namespace std;

int main(int argc, char **argv)
{
    cout << "This is test" << endl;
    testing::InitGoogleTest(&argc, argv);
    return 0;
}

這是生成文件:

INCLUDE = -I/usr/include/
LIBPATH = -L/usr/lib/

output: main.o 
    g++ main.o  -o exampleOutput

main.o: main.cpp
    g++ -c main.cpp $(INCLUDE)  $(LIBPATH) -lgtest -lgtest_main  -pthread

(Gtest 的)頭文件位於/usr/include/gtest ,lib 文件位於/usr/lib

我究竟做錯了什么?

謝謝。

-lgtest-lgtest_main參數應該在鏈接 exampleOutput 時傳遞,而不是在編譯 main.o 時傳遞。 它與您評論中的命令一起使用的原因是該命令同時執行兩個步驟,而您的 makefile 不是。

makefile 也是不正確的,因為目標僅命名為output而命令實際上生成exampleOutput ,因此即使不需要此命令也將始終執行,因為它期望的名為output的文件永遠不會實際生成.. .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM