I am to compile a multi-file C++ program, and have the following error;
Undefined symbols for architecture i386:
"included::printMSG()", referenced from:
_main in ccQwMj1l.o
ld: symbol(s) not[Finished in 0.3s with exit code 1] found for architecture i386
collect2: ld returned 1 exit status
My code:
main.cpp: https://gist.github.com/3845822
included.cpp: https://gist.github.com/3845825
included.h: https://gist.github.com/3845827
(confirmed to work with g++
)
EDIT: I am not putting these files in a project, they are just all in the same folder, and I'm compiling main.cpp
You can't just compile a single file and hope all other files are automatically linked to it to create the final program.
One way to make that happen is to use a program called make
, which reads a Makefile
that has rules that make
follows`.
A simple Makefile
can look like this:
CXXFLAGS = -Wall -g
my_pogram: main.o other_file.o third_file.o
g++ main.o other_file.o third_file.o -o my_program
main.o: main.cpp
g++ $(CXXFLAGS) -c -o main.o main.cpp
other_file.o: other_file.cpp
g++ $(CXXFLAGS) -c -o other_file.o other_file.cpp
third_file.o: third_file.cpp
g++ $(CXXFLAGS) -c -o third_file.o third_file.cpp
There are other similar programs to handle this. One that is popular is CMake.