Error Messages
Understanding the compilation and linking processes is really useful for interpreting error messages!
If you are compiling a source file (.cpp
) and an error message complains about…
- A missing declaration: Maybe you are missing a
#include
(or you've misspelled something) - Multiple definitions: Maybe a header does not have an include guard
If you are linking object files (.o
) together to create an executable…
- A missing definition: You may not have given
clang++
all of the.o
files it needs (or maybe you declared something but didn't define it anywhere) - Multiple definitions: You may have defined the same function in two different
.cpp
files
Guess the Problem!
I was trying to compile cowchat.cpp
into cowchat.o
but I got this error:
cowchat.cpp:14:9: error: use of undeclared identifier 'printMoos'
printMoos(nMoos);
^
1 error generated.
I was trying to create the executable file cowchat
but I got this error:
duplicate symbol 'printMoos(unsigned long)' in:
printmoos.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I was trying to create the executable file cowchat
but I got this error:
Undefined symbols for architecture x86_64:
"printMoos(unsigned long)", referenced from:
_main in cowchat.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I was trying to compile circles.cpp
into circles.o
but I got this error:
In file included from circles.cpp:2:
In file included from ./tau.hpp:1:
./constants.hpp:1:18: error: redefinition of 'PI'
constexpr double PI = 3.1415926535897;
^
circles.cpp:1:10: note: './constants.hpp' included multiple times, additional
include site here
#include "constants.hpp"
^
./tau.hpp:1:10: note: './constants.hpp' included multiple times, additional
include site here
#include "constants.hpp"
^
./constants.hpp:1:18: note: unguarded header; consider using #ifdef guards or
#pragma once
constexpr double PI = 3.1415926535897;
^
1 error generated.
What does
#pragma once
mean and why should we consider using it?That's a nonstandard preprocessor directive meant to replace include guards.
The
once
means that the file should only be included once.Some compilers support it and some don't. It's not part of the C++ standard so we won't use it in CS 70.
(When logged in, completion status appears here.)