Undefined reference in a simple makefile test CPP - c++

I was trying to make a simple makefile to learn how to use headers and make in C++, however it doesn't seems to work, returning "undefined reference" at the end. What is going wrong here? I don't believe there are any mistakes in the .cpp or .hpp files, and I searched a bit about makefile to be sure that it should be working.
My main.cpp:
#include <iostream>
#include <fstream>
#include "test.hpp"
using namespace std;
int main(int argc, char *argv[]){
test::print();
return 0;
}
For test.hpp and test.cpp:
#include <iostream>
#include <fstream>
#pragma once
class test{
private:
public:
static void print();
};
#include <iostream>
#include <fstream>
#include "test.hpp"
using namespace std;
static void print(){
cout << "aaaaaa\n";
}
Finally, the makefile:
all:
rm *.o
g++ -c -g *.cpp
g++ *.o -o main.exe
rm *.o

Related

Undefined reference to class from namespace in C++

I'm new to C++ and trying to do a small quant project with paper trading.
I have a header file alpaca/client.h as follows:
#pragma once
#include <iostream>
#include <../utils/httplib.h>
#include <config.h>
using namespace std;
namespace alpaca {
class Client {
private:
alpaca::Config* config;
public:
Client();
string connect();
};
}
The implementation in alpaca/client.cpp is
#include <iostream>
#include <string>
#include <client.h>
#include <httplib.h>
using namespace std;
namespace alpaca {
Client::Client() {
config = &alpaca::Config();
};
string Client::connect() {
httplib::Client client(config->get_url(MARKET));
auto res = client.Get("/v2/account");
if (res) {
return res->body;
}
else {
return "Error in Client::get_account(): " + to_string(res->status);
}
};
}
And my main.cpp is:
#include <iostream>
#include <string>
#include <client.h>
using namespace std;
int main()
{
alpaca::Client client = alpaca::Client();
client.connect();
return 0;
}
However, I see the following error when I try to compile with g++:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\shubh\AppData\Local\Temp\cc765kwL.o:main.cpp:(.text+0x1ca): undefined reference to 'alpaca::Client::Client()'
Could anyone help with what exactly I'm missing? I'm not too sure.
The g++ command I use is g++ -I./src/alpaca src/main.cpp
It looks like you forgot to compile the client.cpp file. The error message is saying that the linker cannot find a definition for the Client class constructor.
Try compiling both main.cpp and client.cpp with the g++ command, like this:
g++ -I./src/alpaca src/main.cpp src/alpaca/client.cpp

C++ Constructor not showing output

I have the following 3 files, main.cpp and then a class defintion including a header file for the class:
main.cpp:
#include <iostream>
#include "data_vars_class.hpp"
int main () {
DataVars dataVars();
return 0;
}
data_vars_class.hpp:
#include <string>
#include <vector>
class DataVars
{
private:
std::vector<std::string> csv_card_names;
public:
DataVars();
void getCSVData();
};
data_vars_class.cpp:
#include <iostream>
#include <vector>
#include <string>
#include "data_vars_class.hpp"
DataVars::DataVars()
{
std::cout << "constructor?";
getCSVData();
}
void DataVars::getCSVData()
{
std::cout << "Getting csv data!";
}
The problem is when I build and execute the code, I just get an empty terminal. I know that both data_vars_class.hpp and data_vars_class.cpp are being included with the build, this is my build command in Geany:
g++ main.cpp data_vars_class.cpp -o a.out
How come i'm not seeing the cout output in the terminal, like in the constructor shouldnt i see "constructor?" in the terminal?
Thanks
by building the binary file gets created to run the code you have to write ./a.out in the terminal after building it.
g++ main.cpp data_vars_class.cpp -o a.out
./a.out

Linking shared libraries with executables

I have a small doubt in the compilation of a c++ code along with a shared library.
So I have two files main.cpp and sample.cpp.
main.cpp
#include <iostream>
using namespace std;
#include "sample.h"
myStruct obj;
void populateData() {
obj.s = "hello world";
}
myStruct giveData() {
cout << "Inside main: " << obj.s << endl;
return obj;
}
int main() {
populateData();
}
sample.h
#ifndef SAMPLE_H
#define SAMPLE_H
#include <string>
struct myStruct {
std::string s;
void populateData();
};
myStruct giveData();
#endif
sample.cpp
#include "sample.h"
#include <iostream>
#include <boost/python.hpp>
using namespace std;
void myStruct :: populateData() {
cout << giveData().s;
}
BOOST_PYTHON_MODULE(boosts) {
using namespace boost::python;
class_<myStruct>("struct")
.add_property("s", &myStruct::s)
.def("populateData", &myStruct::populateData)
;
}
I compile the program using
g++ -c -fPIC sample.cpp
g++ -c -fPIC main.cpp
g++ -shared -Wl,-soname,boosts.so -o boosts.so sample.o main.o -lpython2.7 -lboost_python
g++ -o main main.o
./main
Now, when I run the main, it populates the string inside the obj. But when I run a python script, that imports the boosts.so, the obj.s is empty.
I am guessing it is because the library boosts.so is not properly linked with the executable main.
How do I fix this?

ctime std:: namespace conflict

I have project with many C and C++ files. I try to add thread-safe queue.
In my header:
#include <queue>
#include <mutex>
#include <thread>
#include <condition_variable>
// Some code..
When I try to compile this, its fault with this errors:
In file included from /usr/include/c++/4.9/chrono:41:0,
from /usr/include/c++/4.9/mutex:39,
from queue.hpp:4,
from main.cpp:24:
/usr/include/c++/4.9/ctime:60:11: error: '::clock_t' has not been declared
using ::clock_t;
/usr/include/c++/4.9/condition_variable:161:23: error: 'time_t' in namespace 'std' does not name a type
static_cast<std::time_t>(__s.time_since_epoch().count()),
As I understand it, compiler try to find std::time_*, but why? And how fix it?
Thanks!
UPD: main.cpp
#include "gpu.hpp" //Error here
int main(int argc, char const *argv[]) {
return 0;
}
gpu.hpp
#pragma once
#include "filter.hpp"
#include "queue.hpp" //Error here
#include <nvcuvid.h>
#include <avformat.h>
#include <vector>
queue.hpp
#pragma once
#include <queue>
#include <mutex>
#include <thread>
#include <condition_variable>
template<typename T>
class CQueue
{
std::queue<T> m_queue;
std::mutex m_mutex;
std::condition_variable m_cond;
// ...
First error message:
In file included from queue.hpp:3:0,
from gpu.hpp:3,
from main-test.cpp:2:
/usr/include/c++/4.9/ctime:60:11: error: '::clock_t' has not been declared
using ::clock_t;
Makefile:
FFMPEG_INCLUDES := -I$(FFMPEG_PATH) $(FFMPEG_PATH)/libavutil ...
$(OBJECTS_DIRS)/app-main-test.o: src/app/main-test.cpp
$(CXX) $(CXXFLAGS) $(FFMPEG_INCLUDES) $(CUDA_INCLUDES) -o $# -c $<
The problem was in my Makefile.
I has include path to each ffmpeg folder. FFMPEG_INCLUDES := -I$(FFMPEG_PATH) $(FFMPEG_PATH)/libavutil ... FFMPEG have time.c in ffmpeg/libavutil It causes conflict with ctime.
I replaced #include <log.h> to #include<libavutil/log.h> and fixed include path in makefile FFMPEG_INCLUDES := -I$(FFMPEG_PATH)
Thank you #user2807083 for help.

C++ does not name a type in Constructor definition

I try to compile my code, I pretty sure I made a mistake in my headers or in the compilation but I don't understand where. I know that this is a basic problem, and I read some other topic, but I really don't understand. And I watch some other code I wrote and I don't see any difference...
g++ -c main.cpp -o out
I don't understand the error, so I also try :
g++ -c readFastqFile.cpp
The error
readFastqFile.cpp:8:1: error: ‘readFastq’ does not name a type
readFastq::readFastq(){ //Constructor
And my files are :
main.cpp
#include <iostream>
#include <string>
#include "readFastqFile.hpp"
using namespace std;
int main(int argc, char *argv[]){
cout << "hello" <<endl;
//readFastq allReads;
return 0;
}
readFastqFile.hpp
#ifdef READFASTQFILE_HPP
#define READFASTQFILE_HPP
#include <iostream>
#include <string>
using namespace std;
class readFastq{
public:
readFastq(); //constructor
private:
string readName;
string sequence;
string score;
};
#endif // READFASTQFILE_HPP
readFastqFile.cpp
#include <string>
#include <iostream>
#include "readFastqFile.hpp"
using namespace std;
readFastq::readFastq(){ //Constructor
readName = "bla";
cout << readName <<endl;
}
Thanks
#ifdef READFASTQFILE_HPP should be #ifndef READFASTQFILE_HPP. The #ifdef is causing the contents of readFastqFile.hpp to be ignored, so the class definition isn't being compiled.
See also Include guards