undefined reference to `PerformChat(char*, char*, char*, char*, char*) - c++

I want to use ChatScript externally in my program. In the documents it says:
Embedding Step #1 First, you will need to modify `common.h and compile the system. You need to add all the CS .cpp files to your build list.
Find the // #define NOMAIN 1 and uncomment it. This will allow you to compile your program as the main program and ChatScript merely as a collection of routines to accompany it.
But I am newbie in Linux and can’t understand how to add .cpp files to my build list? What is my build list? May someone explains what do should I do exactly?
I did copy all the .cpp and .h and other folders existed inside ChatScript/SRC directory beside my main.cpp in my project.
Then I tried to run this code:
#include<iostream>
using namespace std;
char* output2;
unsigned int InitSystem(int argc,char* argv[],char* unchangedPath,char* readonlyPath,char* writablePath);
void InitStandalone();
void PerformChat(char* user,char* usee,char* incoming,char* ip,char* output);
int main()
{
PerformChat(NULL,NULL,"hi",NULL,output2);
cout<<output2;
return 0;
}
But I get this error message:
undefined reference to `PerformChat(char*, char*, char*, char*, char*)
Then I did include all the header files to my program and delete this line of code: void PerformChat(char* user,char* usee,char* incoming,char* ip,char* output);
#include<iostream>
#include "common.h"
#include "common1.h"
#include "constructCode.h"
#include "cs_ev.h"
#include "csocket.h"
#include "dictionaryMore.h"
#include "dictionarySystem.h"
#include "english.h"
#include "evserver.h"
#include "factSystem.h"
#include "functionExecute.h"
#include "infer.h"
#include "jsmn.h"
#include "json.h"
#include "mainSystem.h"
#include "markSystem.h"
#include "mongodb.h"
#include "mprintf.h"
#include "multi.h"
#include "my_sql.h"
#include "os.h"
#include "outputSystem.h"
#include "patternSystem.h"
#include "postgres.h"
#include "privatesrc.h"
#include "scriptCompile.h"
#include "spellcheck.h"
#include "systemVariables.h"
#include "tagger.h"
#include "testing.h"
#include "textUtilities.h"
#include "tokenSystem.h"
#include "topicSystem.h"
#include "userCache.h"
#include "userSystem.h"
#include "variableSystem.h"
using namespace std;
char* output2;
unsigned int InitSystem(int argc,char* argv[],char* unchangedPath,char* readonlyPath,char* writablePath);
void InitStandalone();
void PerformChat(char* user,char* usee,char* incoming,char* ip,char* output);
int main()
{
PerformChat(NULL,NULL,"hi",NULL,output2);
cout<<output2;
return 0;
}
But the new error says:
error: conflicting declaration of C function ‘int main()'

You would have to include all the chatscript SRC files in your project to get the function PerformChat to compile. But shortly ChatScript will release with library compilations as well.

Related

C++ Linking to separate .cpp file says "multiple definitions"

I'm very new to C++ and could use some help. I'm trying to link a file my_help_fxns.cpp to my main.cpp file so i can use those functions in main.cpp, but when i try linking I get the following error for each function in my_help_fxns:
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Geoff\AppData\Local\Temp\ccaPL79E.o:data_vars_class.cpp:(.text+0x0): multiple definition of `my_help_fxns::print_vector_items_int_type(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<int, std::allocator<int> >)'; C:\Users\Geoff\AppData\Local\Temp\cc0mRP1w.o:main.cpp:(.text+0x0): first defined here
So it says I'm defining twice, but I don't know how to get around this. I have a class called data_vars_class. i include my_help_fxns at the top of data_vars_class.cpp, and use the helper fxns successfully in methods for that class. An instance of the class is created at the top of main.cpp. however if i try to use the helper functions in main() in main.cpp, without declaring "my_help_fxns.cpp" at the top of main.cpp, it says functions arent found, and if i do declare it at the top of main.cpp, i get the duplication error its been declared twice. How can I fix this, thanks!
this is the structure of my project
main.cpp ==>
#include "data_vars_class.hpp"
#include <iostream>
#include <chrono>
#include "my_help_fxns.cpp" <--- including here gives duplication error, but if i dont, its functions not found error
DataVars dataVars;
int main () {
my_help_fxns::pause_program();
return 0;
}
data_vars_class.hpp ==>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
class DataVars
{
private:
...
public:
...
}
data_vars_class.cpp ==>
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>
#include "data_vars_class.hpp"
#include "my_help_fxns.cpp"
...i can use my_help_fxns here with no problem, as an instance of this class is created before main() in main.cpp
my_help_fxns.cpp ==>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
namespace my_help_fxns
{
void pause_program() {
std::string dummy;
std::cout << "Enter to continue..." << std::endl;
std::getline(std::cin, dummy);
}
}
And here is the build command for the file in Geany:
g++ main.cpp data_vars_class.cpp -o a.out
Thanks for your help!
Don't include the my_help_fxns.cpp into the other CPP files since that will effectively define those functions in all the CPP files. This violates the one definition rule.
Instead
create a header file that declares (but not defines) those functions
include that header file in all the CPP files
add my_help_fxns.cpp to the compilation command line
Make changes to your files as described:
main.cpp
#include "data_vars_class.hpp"
#include <iostream>
#include <chrono>
#include "my_help_fxns.hpp" // change file extension from cpp -> hpp
DataVars dataVars;
int main () {
my_help_fxns::pause_program();
return 0;
}
data_vars_class
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <array>
#include "data_vars_class.hpp"
// #include "my_help_fxns.cpp" --> Not required here
And then you may simply run:
g++ -o a.out main.cpp; ./a.out
Gives here:
Enter to continue...
sdfsdfsdfsd // --- INPUT

int Method in C++ Class "Requies and Identifier"

I am attempting to make a class to contain some math operations from a CRC math tables handbook I have, in creating one of the functions I got a strange error I had not seem before. The code for both the cpp and the header are below:
//Header File
#include <iostream>
#include <cmath>
#include <string>
#define int "CRCMathLib_H"
using namespace std;
class CRCMathLib
{
public:
int DoReturn_Totient(int Toter); //Error comes from here when trying to declare as an int
};
//CPP Class File
#include "CRCMathLib.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int CRCMathLib::DoReturn_Totient(int Toter)
{
return 0;
}
//CPP Main File
#include <iostream>
#include <cmath>
#include <string>
#include "CRCMathLib.h"
using namespace std;
int main()
{
return 0;
}
The Main file does not do anything as of yet as this is a completely new file for these operations, I believe this may be a preprocessing error and its not picking up on the int statement as I ran it on another PC with VS and it was able to read the statement. anything would help. Also it was requesting a decleration of the header file, so thats why I placed the int there, is this possibly the issue? removing it returns the error of not having a decleration.
In your .h remove #define int "CRCMathLib_H" which is most probably a typo
replace it by
#include <iostream>
#include <cmath>
#include <string>
#pragma once
The #pragma once ensure you can safely include your .h from the cpp implementation file and the main.cpp
You mis understood include guard protection usually done by
ifndef CRCMathLib_H
#define CRCMathLib_H
// all of you .h file delcaration
#endif
This can be easily replace by the #pragma once statement at the begining of the file
More on this here: https://www.learncpp.com/cpp-tutorial/header-guards/

Fatal error when compiling #include <sys/uio.h> on project [windows]

trying to compile a file for class, using the mingw compiler on windows 10. Compiling with g++ gives me an error stating
\projectFile.o
mingw32-g++.exe -o D:\GitHub\GitRepo\projectFile.exe D:\GitHub\GitRepo\projectFile.o
D:\GitRepo\projectFile.cpp:16:20: fatal error: sys/uio.h: No such file or directory
compilation terminated.
From what ive read this header file
#include <sys/uio.h>
is a unix header and is generally included with most unix build environments. I am working on Windows 10 build and have been unsuccessful in trying to get this to work. Is there a work around for windows using different headers? Is there a while to install this file somehow?
The project is a generalized XML parser that as a student my job is to extract functions from the main file so that they can be reused (OOP design space)
#include <iostream>
#include <iterator>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/io.h>
#include <unistd.h>
#include <errno.h>
#include <vector>
#include <algorithm>
#include <ctype.h>
#include "XMLParser.hpp"
Built on Windows 10 (lastest build) with Mingw-64 (lastest version)
This will not compile for me
This fixed my problem creating this uio.h file in sys directory of mingw64
#ifndef SYS_UIO_H
#define SYS_UIO_H
#include <inttypes.h>
#include <unistd.h>
struct iovec
{
void *iov_base; /* Base address of a memory region for input or output */
size_t iov_len; /* The size of the memory pointed to by iov_base */
};
ssize_t readv(int fildes, const struct iovec *iov, int iovcnt);
ssize_t writev(int fildes, const struct iovec *iov, int iovcnt);
#endif /* SYS_UIO_H */

Error LNK2005: function already defined in MyForm.obj

I'm trying to make a windows form application in Visual Studio C++, but I get this errors after compiling, for each function:
error LNK2005: function already defined in MyForm.obj
These are my files:
Source.cpp
#pragma once
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]//leave this as is
void main() {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Project1::MyForm);
}
MyForm.h
#pragma once
#include <iostream>
#include <string>
#include "Header.h"
namespace Project1 {
//codes of te form
}
Header.h
#pragma once
#include <iostream>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
int n, m;
int size1, size2;
//My functions here
So how can I fix errors?
If you declare and implement a function in a header file (Header.h) and if this file gets included twice, then you'll most likely will get a function already defined error at some point.
This can be fixed by either:
Moving function implementation to a source (cpp) file and only keep it's declaration in the header (h) file
Make the function inline (if it is acceptable), that will remove the error

reordering includes causes runtime error

One of my script file(.scnp)included the following headers as follows.
#include <stdio.h>
#include "kel.h"
#include "process.h"
#include "sigtypes.h"
#include "ScriptInterface.h"
#include "../saf/SAF_Scripts.h"
This type of header files include gives an error in GDB saying
"symbol lookup error: /xxx/xxxx.so: undefined symbol:
_Z16KEL_MEM_AllocateP19KEL_MEM_tPoolHandlej.
Included file after
#include <stdio.h>
#include "ScriptInterface.h"
#include "kel.h"
#include "process.h"
#include "sigtypes.h"
#include "../saf/SAF_Scripts.h"
ScriptInterface.h
extern "C"
{
#include "kel.h"
#include "process.h"
#if defined()
#include "sigtypes.h"
#endif
}
The scripts run successfully. What could be the problem? I have found the responsible method for this problem by using "c++filt" but don't know how to proceed further. Please help