I built googletest following the steps given in this link ->
https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/
Here is my code
1 #include <iostream>
2 #include <vector>
3 #include "gtest.h"
4 //#include <gtest.h>
5
6 using namespace std;
7
8 void expectEqual(int n1, int n2){
9 if(n1 != n2){
10 cerr << "Actual value is " << n1 << ", expected " << n2 <<endl;
11 }
12 }
13
14 int binarySearch(vector <int> &arr, int key){
15
16 int low = 0;
17 int high = arr.size() -1 ;
18 while(low <= high){
19 int mid = (low + high) / 2;
20 if(arr[mid] == key){
21 return mid;
22 } else if (arr[mid] < key){
23 low = mid + 1;
24 }
25 else {
26 high = mid - 1;
27 }
28
29 }
58
59 TEST(BinarySearchTest, EmptyVectorTest) {
60 vector<int> arr;
61 arr.push_back(5);
62 expectEqual(binarySearch(arr, 10), -1);
63 }
64
65
66 /*int main() {
67
68 basicTest();
69 smallVectorTest();
70 emptyVectorTest();
71 return 0;
72 }*/
73
74 int main(int argc, char **argv){
75 ::testing::InitGoogleTest(&argc, argv);
76 return RUN_ALL_TESTS();
77 }
78
This is how I compile it using a bash script ->
1 #!/bin/bash
2
3 INC_DIR=/usr/include/gtest/
4 LIB_DIR=/usr/lib/
5
6 #g++ -std=c++11 -isystem $INC_DIR -pthread -L$LIB_DIR -lgtest -lgtest_main binarySearch.cpp
#gives multiple definition of `main'; error
7 #g++ -std=c++11 -isystem $INC_DIR -pthread $LIB_DIR/libgtest.a binarySearch.cpp
#gives undefined reference to `testing::InitGoogleTest(int*, char**)'
8 g++ -std=c++11 -isystem $INC_DIR -pthread $LIB_DIR/libgtest_main.a binarySearch.cpp
#gives multiple definition of `main'; error
For reference, I am also pasting how LIB and Include path looks -->
storm@storm:~/cpp_ut$ ls /usr/lib/
X11 gnupg
accountsservice gnupg2
apparmor gold-ld
apt groff
bfd-plugins hdparm
binfmt.d init
bolt initcpio
byobu initramfs-tools
cloud-init kernel
cnf-update-db klibc
command-not-found klibc-xcgdUApi-P9SoPhW_fi5gXfvWpw.so
compat-ld language-selector
console-setup libDeployPkg.so.0
cpp libDeployPkg.so.0.0.0
cryptsetup libdmmp.so
dbus-1.0 libdmmp.so.0.2.0
dpkg libgtest.a
dracut libgtest_main.a
storm@storm:~/cpp_ut$ ls /usr/include/gtest/
gtest-death-test.h gtest-message.h gtest-printers.h gtest-test-part.h gtest.h gtest_prod.h
gtest-matchers.h gtest-param-test.h gtest-spi.h gtest-typed-test.h gtest_pred_impl.h internal
Every answer that I found on StackOverflow for a similar issue suggests I use either libgtest.a or libgtest_main.a. Both does not resolve my issue.
I apologize for the long question, but I wanted to include all the possible information.
Aucun commentaire:
Enregistrer un commentaire