Error undefined reference to 'std::cout'
Compile the program with:
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
as cout
is present in the C++ standard library, which would need explicit linking with -lstdc++
when using gcc
; g++
links the standard library by default.
With gcc
, (g++
should be preferred over gcc
)
gcc main.cpp -lstdc++ -o main.o
Undefined reference to static constexpr char[]
Add to your cpp file:
constexpr char foo::baz[];
Reason: You have to provide the definition of the static member as well as the declaration. The declaration and the initializer go inside the class definition, but the member definition has to be separate.
This very simple C++ program using the standard library doesn't compile with GCC
g++ main.cpp
or
gcc main.cpp -lstdc++
Error message Undefined reference for std:string
You haven't included std::string
into your code. It's located in the <string>
header. <string.h>
is a C header which doesn't contain std::string
.
cout << function(); undefined reference. issues with a function
You should pass arguments to the function because you've defined as: double distance(double, double);
So, the solution would be:
#include <iostream>
using namespace std;
double distance(double, double);
int main()
{
cout << distance(10, 10);
return 0;
}
double distance(double rate, double time)
{
return time * rate;
}
Also, it would be better to use std::
before everything that's part of the standard library of C++, instead of typing using namespace std;
at the top of your code.
Read more: Why is "using namespace std;" considered bad practice?
undefined reference to std::ios_base::Init::Init()
Your code compiles fine, you're getting a linker error (ld
is the linker, and it's returning 1 (error)), which is complaining about missing c++ libs.
To fix, you'll need to add the stdc++ lib to your commandline, or use g++.
Replace gcc
with g++
or add -lstdc++
to your gcc command line.
gcc Box.cpp client.cpp -o mainfile -lstdc++
or
g++ Box.cpp client.cpp -o mainfile
This will link the std c++ library with your compiled code. Using g++
, you can omit this step.
Related Topics
Purpose of Returning by Const Value
What Are the Gcc Default Include Directories
Error: Request For Member '..' in '..' Which Is of Non-Class Type
Is Floating-Point Addition and Multiplication Associative
Default Value to a Parameter While Passing by Reference in C++
How to Initialize Const Member Variable in a Class
Eclipse Cdt: Symbol 'Cout' Could Not Be Resolved
Why Does the C++ Stl Not Provide Any "Tree" Containers
What's the Scope of Inline Friend Functions
Getting Std :: Ifstream to Handle Lf, Cr, and Crlf
What's the Difference Between Std::Move and Std::Forward
Are Members of a C++ Struct Initialized to 0 by Default
Is It a Conforming Compiler Extension to Treat Non-Constexpr Standard Library Functions as Constexpr
Are C++ Reads and Writes of an Int Atomic
Generating Combinations in C++