makefiles

I am trying to learn how to use makefiles and decided to start off with a simple program. I load 0-19 into a vector and the print out each element of the vector.

makefile
1
2
3
4
5
6
7
8
9
10
all: program

program: main.o printVec.o
	g++ main.o printVec.o -o program
	
main.o: main.cpp
	g++ -c main.cpp
	
printVec.o: printVec.cpp
	g++ -c printVec.cpp


main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include<vector>
#include"functions.h"


main()
{
	std::vector<int> intVec;
	for(int i=0;i<20;++i)
		intVec.push_back(i);
	printVec(intVec);
}


printVec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include<vector>
#include<iostream>
#include"functions.h"

template<typename T>
void printVec(const std::vector<T> &inputVec)
{
	int size = inputVec.size();
	for(int i=0;i<size;++i)
		std::cout<<inputVec[i]<<' ';
	std::cout<<std::endl;
}


functions.h
1
2
template<typename T>
void printVec(std::vector<T> &);


My compiler is returning the following error:
main.o:main.cpp(.text+0x56) undefined reference to void printVec(std::vector<int, allocator blah> &)



template functions/classes must be implemented in header files. You will need to move the body of printVec to functions.h.

Thank you, it is working now. I have another question though. When the compiler compiles(or whatever the proper term is) main.cpp to main.o, does it also compile functions.h with main.cpp, or does it just compile main.cpp to main.o and needs functions.h to make sure that it is sending the right information?
Topic archived. No new replies allowed.