help! multiple files and "undefined reference to"

Hi everyone,

This is a school project. I haven't even started and I'm stuck. There two .cpp files and a header file that were provided by the teacher. I haven't done anything but copy the files to my computer and when I run it I get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
/home/CS162/lab3/app.o||In function `main':|
app.cpp|| undefined reference to `print(int const*, int)'|
app.cpp|| undefined reference to `insert(int, int, int*, int&)'|
app.cpp|| undefined reference to `insert(int, int, int*, int&)'|
app.cpp|| undefined reference to `insert(int, int, int*, int&)'|
app.cpp|| undefined reference to `insert(int, int, int*, int&)'|
app.cpp|| undefined reference to `insert(int, int, int*, int&)'|
app.cpp|| undefined reference to `print(int const*, int)'|
app.cpp|| undefined reference to `remove(int, int&, int*, int&)'|
app.cpp|| undefined reference to `remove(int, int&, int*, int&)'|
app.cpp|| undefined reference to `print(int const*, int)'|
||error: ld returned 1 exit status|
||=== Build failed: 11 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 

It's the weekend now, so it's too late to ask my teacher, and no one else in my class seems to be having this problem. Here are the files:
app.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
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include "list.h"

using namespace std;

int main()
{
	int aList[ARRAY_CAP];
	int size = 0;
	int val = 0;

	print(aList, size);
	insert(0, 10, aList, size);
	insert(1, 20, aList, size);
	insert(0, 4, aList, size);
	insert(1, 40, aList, size);
	insert(2, 25, aList, size);
	print(aList, size);

	if(!remove(5, val, aList, size))
	{
		cout << "remove failed" << endl;
	}
	else
	{
		cout << val << " is removed!" << endl;
	}

	if(!remove(1, val, aList, size))
	{
		cout << "remove failed" << endl;
	}
	else
	{
		cout << val << " is removed!" << endl;
	}

	print(aList, size);

	return 0;
}

list.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
#include <iostream>
#include "list.h"

using namespace std;

bool insert(int position, int val, int intList[], int& size)
{
	//insert your code here and modify the following line
	return true;
}

bool remove(int position, int& val, int intList[], int& size)
{
	//insert your code here and modify the following line
	return true;
}

void print(const int aList[], int size)
{
	cout << endl << "[ ";

	for(int i = 0; i < size; i++)
	{
		cout << aList[i] << " ";
	}
	cout << "]" << endl;
}

list.h
1
2
3
4
5
6
7
8
9
10
#ifndef LIST_H
#define LIST_H

const int ARRAY_CAP = 100;

bool insert(int position, int val, int intList[], int& size);
bool remove(int position, int& val, int intList[], int& size);
void print(const int intList[], int size);

#endif 


I just don't know enough about programming to see what the problem is. I know the errors mean the program is looking for something that the linker can't find. Is that right? But it's all there, so I don't know why it can't find it.

I would really appreciate any thoughts on this; I'm pulling my hair out.

Thanks!
ng
it looks like you need list.cpp to be seen by the compiler --- that means it needs to be in your project, makefile, or commandline compile options (or the object created from it needs to be linked in, if you compiled it by itself). We need to know what tools / os you are using here to help you do that if you need specifics.
I forgot, there's a makefile, too.
1
2
3
4
5
6
7
8
9
10
11
12
13
CC = g++
CPPFLAGS = -Wall -g -std=c++11
app:	app.o list.o

app.o: 	list.h

list.o:	list.h

.PHONY: clean
clean:	
	$(info -- cleaning the directory --)
	rm -f *.o
	rm -f app

I'm doing this with code::blocks on ubuntu linux.
If you were given a makefile then you are probably supposed to compile it from the command line, not with an IDE like code::blocks. Just make sure the two cpp files, the header and the makefile (called "makefile") are in the same directory and them run the command "make" from that directory.
The following works for me on W7 and mingw32-make.exe instead of make.exe (on Linux you should have a ‘regular’ make.exe):
list.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef LIST_HPP
#define LIST_HPP


constexpr int Array_Cap { 100 };

bool insert(int , int , int* , int&);
bool remove(int , int& , int* , int&);
void print(const int* , int);


#endif // LIST_HPP 


list.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
#include "list.hpp"
#include <iostream>


bool insert(int , int , int* , int&)
{
    //insert your code here and modify the following line
    return true;
}


bool remove(int , int& , int* , int&)
{
    //insert your code here and modify the following line
    return true;
}


void print(const int* const alist, int size)
{
    std::cout << "\n[ ";

    for(int i {}; i < size; ++i)
    {
        std::cout << alist[i] << ' ';
    }
    std::cout << "]\n";
}


main.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
32
33
34
35
36
37
38
39
40
#include "list.hpp"
#include <iostream>


int main()
{
    int alist[Array_Cap];
    int size = 0;

    print(alist, size);
    insert(0, 10, alist, size);
    insert(1, 20, alist, size);
    insert(0, 4, alist, size);
    insert(1, 40, alist, size);
    insert(2, 25, alist, size);
    print(alist, size);

    int val = 0;
    if ( !remove(5, val, alist, size) )
    {
        std::cout << "remove failed\n";
    }
    else
    {
        std::cout << val << " is removed!\n";
    }

    if ( !remove(1, val, alist, size) )
    {
        std::cout << "remove failed\n";
    }
    else
    {
        std::cout << val << " is removed!\n";
    }

    print(alist, size);

    return 0;
}


newGrl.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
OBJS    = main.o list.o
SOURCE  = main.cpp list.cpp
HEADER  = list.hpp
OUT	    = newGrl.exe
CC      = g++
FLAGS   = -c -Werror -Wall -Wextra -Wpedantic -Wshadow -O2
LFLAGS  =
# -g option enables debugging mode 
# -c flag generates object code for separate files


all: $(OBJS)
	$(CC) $(OBJS) -o $(OUT) $(LFLAGS)


main.o: main.cpp
	$(CC) $(FLAGS) main.cpp -std=c++17

list.o: list.cpp
	$(CC) $(FLAGS) list.cpp -std=c++17


# clean
clean:
	rm -f $(OBJS) $(OUT)


Output

>mingw32-make.exe -f newGrl.makefile
g++ -c -Werror -Wall -Wextra -Wpedantic -Wshadow -O2 main.cpp -std=c++17
g++ -c -Werror -Wall -Wextra -Wpedantic -Wshadow -O2 list.cpp -std=c++17
g++ main.o list.o -o newGrl.exe

>newGrl.exe

[ ]

[ ]
0 is removed!
0 is removed!

[ ]

Thank you everyone for the replies!! dutch, you were exactly right about the make file. She intended for us to write and compile this using linux command line. So I tried that, and it compiles just fine.
Topic archived. No new replies allowed.