error adding symbols: Archive has no index; run ranlib to add one

Hello

I have problem when I try to use my library.

1
2
3
4
5
6
7
8
9
10
rain@rain /harjutus $ make test
g++ -Wall -std=c++11 -Iinclude -c tests/geometrytest.cpp -o obj/geometrytest.o 
tests/geometrytest.cpp: In function ‘int main(int, char**)’:
tests/geometrytest.cpp:23:8: warning: unused variable ‘f’ [-Wunused-variable]
  float f = v1.distanceFrom(v1);
        ^
g++ obj/geometrytest.o -o bin/geometrytest -Llib -lgeometry 
lib/libgeometry.a: error adding symbols: Archive has no index; run ranlib to add one
collect2: error: ld returned 1 exit status
make: *** [bin/geometrytest] Error 1


I have include/point.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef POINT_H
#define POINT_H

// Comment this for documentation!

#include <iostream>
#include <list>
#include <sstream>
using std::ostream;

template <unsigned short n>
class Point {

public:
    std::list<float> coords = std::list<float>(n);

    Point() = default; //c++11 The default constructor is explicitly stated.

    Point(std::list<float> p);

    float distanceFrom (Point p);
    
    std::string toString();
};


template <unsigned short n>
Point<n>::Point(std::list<float> arg)
{
	coords.assign (arg.begin(), arg.end());
}


template <unsigned short n>
float Point<n>::distanceFrom (Point p) {
	//return sqrt (pow(x-p.x,2.0) + pow(y-p.y,2.0));
	return 0.0;
}

template <unsigned short n>
std::string Point<n>::toString () {
    std::stringstream ss;

    for (std::list<float>::const_iterator iterator = coords.begin(), end = coords.end(); iterator != end; ++iterator) {
	if(iterator != coords.begin()){
	    ss << ",";
	}
	ss << *iterator;
    }
    
    std::string s = ss.str();
    return s;
}

template <unsigned short n>
ostream& operator<<(ostream& out, const Point<n>& p) {
    out << "(" << "asdf" << ")";
    return out;
}
#endif 


tests/geometrytest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstdlib>
#include <list>
#include "point.h"

using namespace std;

int main (int argc, char* argv[]) {

	// Warning! This test only tests for the presence of methods,
	// not for any kind of validity.

	// Task 1

	// Point<n>
	// Require default constructor
	Point<2> v1;
	// Require member coords
	cout << v1.coords.size() << endl;
	// Require constructor with parameters
	Point<2> v2 { list<float>{} };
	// Require distanceFrom method
	float f = v1.distanceFrom(v1);
	// Require toString method
	cout << v1.toString () << endl;
	// Require << operators
	cout << v1 << endl;


	return EXIT_SUCCESS;
}


Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#####################################################
# You may need to change the parameters under here. #
#####################################################

# Step 1: Choose a compiler. By default, use clang++

# If you use clang++, make sure the following line does not start with a comment (#)
CXX=g++
# If you use g++, uncomment the following line
#CXX=g++

# Set default compiler parameters
# -Wall 	shows all warnings when compiling, always use this!
# -std=c++11 	enables the C++11 standard mode
CXXFLAGS = -Wall -std=c++11

# Step 2: If you use clang++ under Mac OS X, remove these comments
#CXXFLAGS += -stdlib=libc++
#LFLAGS += -stdlib=libc++

# Step 3: Run 'make' in the same directory as this file


############################
# Settings for the library #
############################


# Compiler flag -Idir specifies, that there are includes in the 'dir' directory
LIB_CXXFLAGS = $(CXXFLAGS) -Iinclude

# List of objects for the library
LIBOBJS = obj/point.o

# Name of the library
LIBRARY = lib/libgeometry.a

################################
# Settings for the testing app #
################################

# Compiler flags for the testing app
APP_CXXFLAGS = $(CXXFLAGS) -Iinclude

# Linker flags (order the compiler to link with our library)
LFLAGS += -Llib -lgeometry

# The object for the testing app
TESTOBJS = obj/geometrytest.o

# The name of the testing app
TESTAPP = bin/geometrytest

# This is the first target. It will be built when you run 'make' or 'make build'
build: $(LIBRARY)

# Create the library by using 'ar'
$(LIBRARY) : $(LIBOBJS)
	ar cr $(LIBRARY) $(LIBOBJS)

# Compile each source file of the librar
obj/point.o: include/point.h
	$(CXX) $(LIB_CXXFLAGS) -c include/point.h -o obj/point.o

# Rule for linking the test app with our library
$(TESTAPP): $(TESTOBJS) $(LIBRARY)
	$(CXX) $(TESTOBJS) -o $(TESTAPP) $(LFLAGS) 

# Compile each source file of the library
obj/geometrytest.o: tests/geometrytest.cpp
	$(CXX) $(APP_CXXFLAGS) -c tests/geometrytest.cpp -o obj/geometrytest.o 

test: $(TESTAPP)

doc:
	doxygen

clean:
	rm -f $(LIBOBJS)
	rm -f $(TESTOBJS)
	rm -f $(LIBRARY)
	rm -f $(TESTAPP)
	rm -f ./bin/* */

What causes this error and how could I fix this error? Could it be because I only have .h file?
Last edited on
You are trying to compile a header (point.h), you should compile source files, not headers.

Also, it contains template functions, but you never instantiate anyone, so the result object file is empty. http://www.cplusplus.com/forum/general/113904/#msg622073
You may do $ nm geometry.a and will see that it has nothing.


To fix it, your "library" it would not be an object file, but simply the header. So just $ g++ obj/geometrytest.o -o bin/geometrytest should work.
As an alternative you may try explicit template instantiation.


By the way
1
2
# If you use clang++, make sure the following line does not start with a comment (#)
CXX=g++ #¿shouldn't that be clang++? 
So creating empty point.cpp that includes point.h would work?

My task requires me to build module. But since I need templated class I use .h only for that class.
No, in that case the resulting object file would be empty.


> My task requires me to build module.
I don't know what you are talking about.
Topic archived. No new replies allowed.