Where is the undefined reference? [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I have a problem!
It shows this error:
"C:\Users\User\AppData\Roaming\JetBrains\CLion 2018.2.4\bin\cmake\win\bin\cmake.exe" --build C:\Users\User\CLionProject\HospitalTest\cmake-build-debug --target HospitalTest -- -j 2
[ 50%] Linking CXX executable HospitalTest.exe
CMakeFiles\HospitalTest.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/User/CLionProject/HospitalTest/main.cpp:9: undefined reference to `CHospitalWard::CHospitalWard()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:23: undefined reference to `CHospitalWard::OnAdd()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:24: undefined reference to `CHospitalWard::OnDelRegNum()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:25: undefined reference to `CHospitalWard::OldestPatient()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:26: undefined reference to `CHospitalWard::OnPrint()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:27: undefined reference to `CHospitalWard::IsInRegNum()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\HospitalTest.dir\build.make:85: HospitalTest.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/HospitalTest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/HospitalTest.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: HospitalTest] Error 2
Code:
///////////////////////////////
#pragma once
#include "Patient.h"
#include <string>
using namespace std;
class CHospitalWard
{
private:
string m_name;
int m_br;
CPatient *m;
public:
CHospitalWard();
CHospitalWard(string, int);
~CHospitalWard(){delete []m;}
void OnAdd();
void OnDelRegNum();
void OnPrint();
int IsInRegNum();
void OldestPatient();
string name_access() {return m_name;}
int br_access() {return m_br;}
};
There are declared like this:
cout<<"Generating..."<<endl;
CHospitalWard f;
int c;
string s;

It is possible that you have included this file, but you have not yet written implementations for these functions. You will need to implement all functions in order for the program to compile.
The minimum implementation for function void DoNothing(); for example would be,
void DoNothing() {}

Related

Why can't I use multithreading in my project?

I want to start with multithreading, but my clion IDE (v. 2019.3.6) does not recognize created threads.
I also try to use threads from boost library, but there is also common error ("undefined reference to boost::threads" is there). I use c++14 in my clion and mingw compilator.
I compiled boost library using g++.
Code I try to run:
#include <iostream> // std::cout
#include <boost/thread.hpp>
void foo(){
// do stuff...
}
void bar(int x){
// do stuff...
}
int main(){
boost::thread t1(bar, 1);
t1.join();
return 0;
}
or:
#include <iostream> // std::cout
#include <thread>
void foo(){
// do stuff...
}
void bar(int x){
// do stuff...
}
int main(){
std::thread t1(bar, 1);
t1.join();
return 0;
}
errors which it generates:
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::thread::start_thread()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:182: undefined reference to `boost::thread::start_thread_noexcept()'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::thread::~thread()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:257: undefined reference to `boost::thread::detach()'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::thread::join()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:759: undefined reference to `boost::thread::get_id() const'
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:759: undefined reference to `boost::this_thread::get_id()'
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:762: undefined reference to `boost::thread::join_noexcept()'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(int), boost::_bi::list1<boost::_bi::value<int> > > >::~thread_data()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:94: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
CMakeFiles\client.dir/objects.a(client.cpp.obj):client.cpp:(.rdata$.refptr._ZTVN5boost6detail16thread_data_baseE[.refptr._ZTVN5boost6detail16thread_data_baseE]+0x0): undefined reference to `vtable for boost::detail::thread_data_base'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\client.dir\build.make:86: client.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/client.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: client] Error 2
or errors thread from std::thread
C:\Users\Admin\Desktop\client\client.cpp: In function 'int main()':
C:\Users\Admin\Desktop\client\client.cpp:15:10: error: 'thread' is not a member of 'std'
std::thread t1(bar, 1);
^~~~~~
C:\Users\Admin\Desktop\client\client.cpp:15:10: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
C:\Users\Admin\Desktop\client\client.cpp:5:1:
+#include <thread>
C:\Users\Admin\Desktop\client\client.cpp:15:10:
std::thread t1(bar, 1);
^~~~~~
C:\Users\Admin\Desktop\client\client.cpp:16:5: error: 't1' was not declared in this scope
t1.join();
^~
C:\Users\Admin\Desktop\client\client.cpp:16:5: note: suggested alternative: 'tm'
t1.join();
^~
tm
mingw32-make.exe[3]: *** [CMakeFiles\client.dir\build.make:63: CMakeFiles/client.dir/client.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/client.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: client] Error 2
Why thread is not memeber of std::thread?
I have attached proper header
Is this possible that mingw does not support multithreading?
Maybe someone had problem like this and will be able to help.
The mingw didn't recognize thread library.
I changed version of compilator to another and everything works nice.

Tensorflow c++ api undefined reference to `tflite::DefaultErrorReporter()'

I successfully built tensorflow_cc on Ubuntu 16.04 with a static build.
I was testing a small code I took from tflite guide:
$> cat test1.cpp
#include <cstdio>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
using namespace tflite;
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "minimal \n");
return 1;
}
const char* filename = argv[1];
std::unique_ptrtflite::FlatBufferModel model =
tflite::FlatBufferModel::BuildFromFile(filename);
return 0;
}
I am getting this error:
[ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o
[100%] Linking CXX executable example
CMakeFiles/example.dir/example.cpp.o: In function main': example.cpp:(.text+0x57):
undefined reference to tflite::DefaultErrorReporter()'
example.cpp:(.text+0x6d): undefined reference to
tflite::FlatBufferModel::BuildFromFile(char const*, tflite::ErrorReporter*)'
CMakeFiles/example.dir/example.cpp.o: In function
std::default_deletetflite::FlatBufferModel::operator()(tflite::FlatBufferModel*) const':
example.cpp
(.text.ZNKSt14default_deleteIN6tflite15Flat
BufferModelEEclEPS1[ZNKSt14default_deleteIN6tflite15FlatBufferModelEEclEPS1]+0x1e):
undefined reference to `tflite::FlatBufferModel::~FlatBufferModel()'
collect2: error: ld returned 1 exit status
CMakeFiles/example.dir/build.make:85: recipe for target 'example' failed
make[2]: *** [example] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/example.dir/all' failed
make[1]: *** [CMakeFiles/example.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Any suggestions on how to proceed?
It's answered on https://github.com/tensorflow/tensorflow/issues/36661
By some strange reason, you need to provide your object file first than tflite library to your linker.
FYI https://eli.thegreenplace.net/2013/07/09/library-order-in-static-linking

Undefined reference to the static variable of structure in cpp [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
static variable link error [duplicate]
(2 answers)
Closed 3 years ago.
I am not able to use structures for static variable and function. Could anyone explain why exactly is this behavior?
#include <iostream>
using namespace std;
class Foo
{
private:
typedef struct
{
int bee_1;
}bee;
static bee test;
public:
static void Inc(){ test.bee_1++;}
static int getBee_1(){return test.bee_1;}
};
int main(){
Foo::Inc();
Foo::Inc();
Foo::Inc();
Foo::Inc();
cout << Foo::getBee_1();
return 0;
}
I get the following error when I use this code:
clang++-7 -pthread -o main main.cpp
/tmp/main-521333.o: In function `Foo::Inc()':
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x24): undefined reference to `Foo::test'
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x2e): undefined reference to `Foo::test'
/tmp/main-521333.o: In function `Foo::getBee_1()':
main.cpp:(.text._ZN3Foo8getBee_1Ev[_ZN3Foo8getBee_1Ev]+0x7): undefined reference to `Foo::test'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1

Why std::make_shared different with new when construct with static constexpr member? [duplicate]

This question already has answers here:
Undefined reference error for static constexpr member
(2 answers)
Closed 5 years ago.
#include <iostream>
#include <vector>
#include <memory>
class Node{
public:
static constexpr int data_size = sizeof(int);
};
class View{
public:
View(int size){
}
};
class Header: public Node{
public:
void foo(){
std::shared_ptr<View> v = std::make_shared<View>(data_size);
}
void bar(){
std::shared_ptr<View> v(new View(data_size));
}
View bar1(){
return View(data_size);
}
void bar2(){
View *v = new View(data_size);
}
int bar3(){
return data_size;
}
};
int main() {
Header *h = new Header();
// This 1 lines below will produce the error
h->foo();
// These 4 lines are ok
h->bar();
h->bar1();
h->bar2();
h->bar3();
return 0;
}
When call foo() , error below will occur :
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/everettjf/code/cpptest/cmake-build-debug --target all -- -j 8
Scanning dependencies of target cpptest
[ 50%] Building CXX object CMakeFiles/cpptest.dir/main.cpp.o
[100%] Linking CXX executable cpptest
Undefined symbols for architecture x86_64:
"Node::data_size", referenced from:
Header::foo() in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cpptest] Error 1
make[1]: *** [CMakeFiles/cpptest.dir/all] Error 2
make: *** [all] Error 2
When I use new to initialize the shared_ptr ,it is ok.But when I use make_shared, the link error occurs.
Why std::make_shared different with new when construct with static constexpr member ?
My environment is macOS with Clion, CMakeList.txt is :
cmake_minimum_required(VERSION 3.6)
project(cpptest)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(cpptest ${SOURCE_FILES})
Why new is working..
std::shared_ptr<View> v(new View(data_size));
In the above code (my guess is) the compiler substitutes data_size with the value itself. So it compiles and runs fine.
Why std::make_shared<>() fails ..
std::shared_ptr<View> v = std::make_shared<View>(data_size);
std::make_shared does something called perfect forwarding. And in the above case it needs to know the address of Node::data_size. Since you have not defined it anywhere, you linker can't find the symbols and throws the error.
To solve this problem, define the symbol Node::data_size in your code.

Nasty error when initializing vector

I am just trying to make a vector, but it gives me a huge error and I am following a working example from my other project. The code:
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct organism {
bool One;
bool Two;
};
std::vector<organism> organisms;
int main() {
printf("Content-type: text/html\n\n");
printf("TEST");
printf(getenv("QUERY_STRING"));
return 0;
}
The error:
> "make"
C:/MinGW/bin/gcc.exe -o build/e2.exe source/main.cpp
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.text$_ZN9__gnu_cxx13new_allocatorI8organismE10deallocateEPS1_j[__gnu_cxx::new_allocator<organism>::deallocate(organism*, unsigned int)]+0xd): undefined reference to `operator delete(void*)'
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.eh_frame$_ZNSt12_Vector_baseI8organismSaIS0_EED2Ev+0x13): undefined reference to `__gxx_personality_v0'
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.eh_frame$_ZNSt6vectorI8organismSaIS0_EED1Ev+0x13): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
"make": *** [build] Error 1
> Process Exit Code: 2
> Time Taken: 00:01
I can compile it if I comment out std::vector<organism> organisms; but I have no clue what's wrong with that line. It's exactly the same in my other project, which compiles fine.
You need to compile with g++.exe instead of gcc.exe so that it will know it needs to link with the C++ library.