Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please look at this github that explains how to use ChatScript as a library inside a C/C++ program(only see Embedding step1 and 2): [^]

As the tutorial says, I must do 2 calls.
1. InitSystem() function call to initialize the library.
2-performChat() function to pass a string to library and get the output string as the answer.

What I have tried:

So I wrote this simple C++ code:

#include <iostream>
#include "common.h"
#include "mainSystem.h"

using namespace std;


int main(int argc, char * argv[])
{
char* input="hi";
char* output;
char * userID="one";

InitSystem (0, NULL);
void InitStandalone();

PerformChat("one","Harry",input,NULL,output);

cout<<output;

    return 0;
}


And I know both the mentioned functions are inside of mainSystem.cpp, so I did include that file to my program.(this is mainSystem.h : https://github.com/bwilcox-1234/ChatScript/blob/master/SRC/mainSystem.h and this is mainSystem.cpp https://github.com/bwilcox-1234/ChatScript/blob/master/SRC/mainSystem.cpp)

Everything looks OK but when I try to compile my code by
C++
g++ -o mayprogram main.o
it says:

main.o: In function `main':
main.cpp:(.text+0x44): undefined reference to `InitSystem(int, char**, char*, char*, char*, USERFILESYSTEM*, void (*)(char*), void (*)(char*))'
main.cpp:(.text+0x6a): undefined reference to `PerformChat(char*, char*, char*, char*, char*)'
collect2: error: ld returned 1 exit status


It's very strange for me!!! Why do I get this error message when I included the mainSystem.h above my code?

***about main.o I must say I built it by running
C++
make standalone
command before, with my
C++
main.cpp
successfully!

This is the make file:

.PHONY = clean binary server debugserver standalone pgserver debugpgserver mysqlserver debugmysqlserver mongoserver debugmongoserver foreignserver mongotreetaggerserver 

# Removed the 'all' target.  It does not  make sense since each build target requires a different set of defines.
# So we need to clean before building each target in order compile in the right stuff.  
#
# E.g. to build pg and mysql:
#
# 1. make clean pgserver
# 2. make clean mysqlserver

# Note: to build mysql targets on Ubuntu:
# sudo apt-get install libmysqlclient-dev

# store executable names in make variables so that cleanall target works
executable.server=../BINARIES/ChatScript
server: DEFINES+= -DLOCKUSERFILE=1  -DEVSERVER=1 -DEVSERVER_FORK=1  -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1 
server: PGLOAD= -pthread
server: INCLUDEDIRS=-Ievserver
server: binary
server: EXECUTABLE=$(executable.server)
server: CFLAGS=-c  -std=c++11 -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing 

executable.debugserver=../BINARIES/ChatScriptDebug
debugserver: DEFINES+= -DLOCKUSERFILE=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1  -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1 
debugserver: PGLOAD= -pthread
debugserver: INCLUDEDIRS=-Ievserver
debugserver: binary
debugserver: EXECUTABLE=$(executable.debugserver)
debugserver: CFLAGS=-c -std=c++11 -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g

executable.standalone=../BINARIES/CS_LINUX
standalone: DEFINES+=  -DDISCARDSERVER=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1 -DEVSERVER=1 -DEVSERVER_FORK=1
standalone: PGLOAD= -pthread
standalone: INCLUDEDIRS=-Ievserver
standalone: binary
standalone: EXECUTABLE=$(executable.standalone)
standalone: CFLAGS=-c -std=c++11  -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing 

executable.pgserver=../BINARIES/ChatScriptpg
pgserver: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1
pgserver: PGLOAD= -lpq -pthread
pgserver: binary
pgserver: EXECUTABLE=$(executable.pgserver)
pgserver: INCLUDEDIRS=-Ievserver -Ipostgres
pgserver: CFLAGS=-c -std=c++11 -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing 

executable.debugpgserver=../BINARIES/ChatScriptpgDebug
debugpgserver: DEFINES+= -DLOCKUSERFILE=1  -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1  -DDISCARDMONGO=1 -DDISCARDMYSQL=1
debugpgserver: PGLOAD= -lpq -pthread
debugpgserver: INCLUDEDIRS=-Ievserver -Ipostgres 
debugpgserver: binary
debugpgserver: EXECUTABLE=$(executable.debugpgserver)
debugpgserver: CFLAGS=-c -std=c++11 -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g

executable.mysqlserver=../BINARIES/ChatScriptMysql
mysqlserver: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1
mysqlserver: PGLOAD= -lmysqlclient -pthread
mysqlserver: binary
mysqlserver: EXECUTABLE=$(executable.mysqlserver)
mysqlserver: INCLUDEDIRS=-Ievserver 
mysqlserver: CFLAGS=-c -std=c++11 -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing 

executable.debugmysqlserver=../BINARIES/ChatScriptMysqlDebug
debugmysqlserver: DEFINES+= -DLOCKUSERFILE=1  -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1  
debugmysqlserver: PGLOAD= -lmysqlclient -pthread 
debugmysqlserver: INCLUDEDIRS=-Ievserver
debugmysqlserver: binary
debugmysqlserver: EXECUTABLE=$(executable.debugmysqlserver)
debugmysqlserver: CFLAGS=-c -std=c++11 -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g

executable.mongoserver=../BINARIES/ChatScriptMongo
mongoserver: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMYSQL=1
mongoserver: PGLOAD= -pthread
mongoserver: binary
mongoserver: EXECUTABLE=$(executable.mongoserver)
mongoserver: INCLUDEDIRS=-Iinclude -I/usr/local/include/libbson-1.0 -I/usr/local/include/libmongoc-1.0
mongoserver: CFLAGS=-c -std=c++11  -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g

executable.debugmongoserver=../BINARIES/ChatScriptMongoDebug
debugmongoserver: DEFINES+= -DLOCKUSERFILE=1  -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1  -DDISCARDPOSTGRES=1  -DDISCARDMYSQL=1
debugmongoserver: PGLOAD= -lpq -pthread
debugmongoserver: binary
debugmongoserver: INCLUDEDIRS=-Iinclude -I/usr/local/include/libbson-1.0 -I/usr/local/include/libmongoc-1.0
debugmongoserver: EXECUTABLE=$(executable.debugmongoserver)
debugmongoserver: CFLAGS=-c -std=c++11 -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g

executable.foreignserver=../BINARIES/ChatScriptForeign
foreignserver: DEFINES+= -DLOCKUSERFILE=1  -DEVSERVER=1 -DEVSERVER_FORK=1  -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1 -DTREETAGGER=1
foreignserver: PGLOAD= -pthread -L../treetagger -L../BINARIES -ltreetagger
foreignserver: INCLUDEDIRS=-Ievserver
foreignserver: binary
foreignserver: EXECUTABLE=$(executable.foreignserver)
foreignserver: CFLAGS=-c -std=c++11 -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing 

executable.mongotreetaggerserver=../BINARIES/ChatScriptMongoForeign
mongotreetaggerserver: DEFINES+= -DLOCKUSERFILE=1  -DEVSERVER=1 -DEVSERVER_FORK=1  -DDISCARDPOSTGRES=1  -DDISCARDMYSQL=1 -DTREETAGGER=1
mongotreetaggerserver: PGLOAD= -pthread -L../treetagger -L../BINARIES -ltreetagger
mongotreetaggerserver: INCLUDEDIRS=-Iinclude -I/usr/local/include/libbson-1.0 -I/usr/local/include/libmongoc-1.0
mongotreetaggerserver: binary
mongotreetaggerserver: EXECUTABLE=$(executable.mongotreetaggerserver)
mongotreetaggerserver: CFLAGS=-c -std=c++11 -Wall  -funsigned-char  -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing 

UNAME := $(shell uname)

CC=g++

ifeq ($(UNAME), Linux)
$(info ************ LINUX VERSION ************)
LDFLAGS= -L/usr/lib64 -lrt -lcurl  --verbose $(PGLOAD)
mongoserver: LDFLAGS+= -lcrypt -lsasl2 -L/usr/local/lib -lmongoc-1.0 -lbson-1.0 
debugmongoserver: LDFLAGS+= -lcrypt -lsasl2 -L/usr/local/lib -lmongoc-1.0 -lbson-1.0 
mongotreetaggerserver: LDFLAGS+= -lcrypt -lsasl2 -L/usr/local/lib -lmongoc-1.0 -lbson-1.0 
endif

ifeq ($(UNAME), Darwin)
OSXRELEASE := $(shell defaults read loginwindow SystemVersionStampAsString)
$(info ************ MAC VERSION $OSXRELEASE detected ************)
LDFLAGS=-mmacosx-version-min=$(OSXRELEASE) -lcurl $(PGLOAD)  
CFLAGS=-mmacosx-version-min=$(OSXRELEASE) -c  -Wall  -funsigned-char -Wno-write-strings -Wno-char-subscripts 
endif

ifeq ($(UNAME), FreeBSD)
$(info ************ FREEBSD VERSION ************)
DEFINES+= -DFREEBSD=1
INCLUDEDIRS+= -I/usr/local/include
LDFLAGS= -L/usr/local/lib -lrt -lcurl -lev  --verbose $(PGLOAD)
mongoserver: LDFLAGS+= -lgcrypt -lsasl2 -lmongoc-1.0 -lbson-1.0 
debugmongoserver: LDFLAGS+= -lgcrypt -lsasl2 -lmongoc-1.0 -lbson-1.0 
mongotreetaggerserver: LDFLAGS+= -lgcrypt -lsasl2 -lmongoc-1.0 -lbson-1.0 
endif

SOURCES=main.cpp constructCode.cpp duktape/duktape.cpp evserver.cpp csocket.cpp cs_ev.c dictionarySystem.cpp englishTagger.cpp factSystem.cpp json.cpp functionExecute.cpp english.cpp infer.cpp javascript.cpp jsmn.cpp markSystem.cpp mysql.cpp mongodb.cpp os.cpp outputSystem.cpp patternSystem.cpp postgres.cpp privatesrc.cpp scriptCompile.cpp spellcheck.cpp secure.cpp systemVariables.cpp tagger.cpp testing.cpp textUtilities.cpp tokenSystem.cpp topicSystem.cpp userCache.cpp userSystem.cpp variableSystem.cpp mainSystem.cpp 
OBJECTS=$(SOURCES:.cpp=.o)

default: server

clean:
	-rm -f *.o

cleanall: clean
	-rm -f $(executable.server)
	-rm -f $(executable.debugserver)
	-rm -f $(executable.standalone)
	-rm -f $(executable.pgserver)
	-rm -f $(executable.debugpgserver)
	-rm -f $(executable.mysqlserver)
	-rm -f $(executable.debugmysqlserver)
	-rm -f $(executable.mongoserver)
	-rm -f $(executable.debugmongoserver)
	-rm -f $(executable.foreignserver)
	-rm -f $(executable.mongotreetaggerserver)

binary: $(OBJECTS)
	$(CC) $(OBJECTS) $(LDFLAGS) $(DEFINES) $(INCLUDEDIRS) -o $(EXECUTABLE)

.cpp.o:
	$(CC) $(CFLAGS) $(DEFINES) $(INCLUDEDIRS) $< -o $@



EDIT: when I try this command:
g++ -std=c++11 -o myprogram main.cpp constructCode.cpp duktape/duktape.cpp evserver.cpp csocket.cpp cs_ev.c dictionarySystem.cpp englishTagger.cpp factSystem.cpp json.cpp functionExecute.cpp english.cpp infer.cpp javascript.cpp jsmn.cpp markSystem.cpp mysql.cpp mongodb.cpp os.cpp outputSystem.cpp patternSystem.cpp postgres.cpp privatesrc.cpp scriptCompile.cpp spellcheck.cpp secure.cpp systemVariables.cpp tagger.cpp testing.cpp textUtilities.cpp tokenSystem.cpp topicSystem.cpp userCache.cpp userSystem.cpp variableSystem.cpp mainSystem.cpp 


It gives me this result:

                                                    ^
/tmp/ccU6n5Cp.o: In function `CurlShutdown()':
json.cpp:(.text+0x1863): undefined reference to `curl_global_cleanup'
/tmp/ccU6n5Cp.o: In function `InitCurl()':
json.cpp:(.text+0x1882): undefined reference to `curl_global_init'
/tmp/ccU6n5Cp.o: In function `UrlEncodePiece(char*)':
json.cpp:(.text+0x18b5): undefined reference to `curl_easy_init'
json.cpp:(.text+0x1902): undefined reference to `curl_easy_escape'
json.cpp:(.text+0x193a): undefined reference to `curl_free'
json.cpp:(.text+0x1946): undefined reference to `curl_easy_cleanup'
/tmp/ccU6n5Cp.o: In function `encodeSegment(char**, char*, char*, void*)':
json.cpp:(.text+0x1a05): undefined reference to `curl_easy_escape'
json.cpp:(.text+0x1a28): undefined reference to `curl_free'
/tmp/ccU6n5Cp.o: In function `JSONOpenCode(char*)':
json.cpp:(.text+0x236d): undefined reference to `curl_easy_init'
json.cpp:(.text+0x255e): undefined reference to `curl_easy_setopt'
json.cpp:(.text+0x257e): undefined reference to `curl_easy_setopt'
json.cpp:(.text+0x25a5): undefined reference to `curl_easy_setopt'
json.cpp:(.text+0x25c5): undefined reference to `curl_easy_setopt'
json.cpp:(.text+0x25ec): undefined reference to `curl_easy_setopt'
/tmp/ccU6n5Cp.o:json.cpp:(.text+0x260c): more undefined references to `curl_easy_setopt' follow
/tmp/ccU6n5Cp.o: In function `JSONOpenCode(char*)':
json.cpp:(.text+0x2620): undefined reference to `curl_slist_append'
json.cpp:(.text+0x2991): undefined reference to `curl_slist_append'
json.cpp:(.text+0x2bc2): undefined reference to `curl_easy_setopt'
json.cpp:(.text+0x2c28): undefined reference to `curl_easy_setopt'
json.cpp:(.text+0x2c8a): undefined reference to `curl_easy_setopt'
json.cpp:(.text+0x2cf7): undefined reference to `curl_easy_setopt'
json.cpp:(.text+0x2d26): undefined reference to `curl_easy_setopt'
/tmp/ccU6n5Cp.o:json.cpp:(.text+0x2d53): more undefined references to `curl_easy_setopt' follow
/tmp/ccU6n5Cp.o: In function `JSONOpenCode(char*)':
json.cpp:(.text+0x2ea7): undefined reference to `curl_easy_perform'
json.cpp:(.text+0x2ecb): undefined reference to `curl_easy_getinfo'
json.cpp:(.text+0x31cf): undefined reference to `curl_slist_free_all'
json.cpp:(.text+0x31de): undefined reference to `curl_easy_cleanup'
/tmp/ccaIe2WK.o: In function `mysqlUserRead(void*, unsigned long, unsigned long, _IO_FILE*)':
mysql.cpp:(.text+0xe5): undefined reference to `mysql_stmt_init'
mysql.cpp:(.text+0x10c): undefined reference to `mysql_error'
mysql.cpp:(.text+0x14e): undefined reference to `mysql_stmt_prepare'
mysql.cpp:(.text+0x16b): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x1fe): undefined reference to `mysql_stmt_bind_param'
mysql.cpp:(.text+0x21b): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x241): undefined reference to `mysql_stmt_execute'
mysql.cpp:(.text+0x25e): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x284): undefined reference to `mysql_stmt_store_result'
mysql.cpp:(.text+0x2a1): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x2c7): undefined reference to `mysql_stmt_num_rows'
mysql.cpp:(.text+0x360): undefined reference to `mysql_stmt_bind_result'
mysql.cpp:(.text+0x37d): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x3a3): undefined reference to `mysql_stmt_fetch'
mysql.cpp:(.text+0x3ea): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x420): undefined reference to `mysql_stmt_error'
/tmp/ccaIe2WK.o: In function `mysqlUserWrite(void const*, unsigned long, unsigned long, _IO_FILE*)':
mysql.cpp:(.text+0x56f): undefined reference to `mysql_stmt_init'
mysql.cpp:(.text+0x596): undefined reference to `mysql_error'
mysql.cpp:(.text+0x5d8): undefined reference to `mysql_stmt_prepare'
mysql.cpp:(.text+0x5f5): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x6cb): undefined reference to `mysql_stmt_bind_param'
mysql.cpp:(.text+0x6e8): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x719): undefined reference to `mysql_stmt_execute'
mysql.cpp:(.text+0x731): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x747): undefined reference to `mysql_stmt_errno'
mysql.cpp:(.text+0x784): undefined reference to `mysql_stmt_affected_rows'
mysql.cpp:(.text+0x7c0): undefined reference to `mysql_stmt_init'
mysql.cpp:(.text+0x7e7): undefined reference to `mysql_error'
mysql.cpp:(.text+0x829): undefined reference to `mysql_stmt_prepare'
mysql.cpp:(.text+0x846): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x91c): undefined reference to `mysql_stmt_bind_param'
mysql.cpp:(.text+0x939): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x95f): undefined reference to `mysql_stmt_execute'
mysql.cpp:(.text+0x97c): undefined reference to `mysql_stmt_error'
mysql.cpp:(.text+0x9a2): undefined reference to `mysql_stmt_affected_rows'
mysql.cpp:(.text+0x9c7): undefined reference to `mysql_stmt_error'
/tmp/ccaIe2WK.o: In function `MySQLUserFilesCode()':
mysql.cpp:(.text+0xa09): undefined reference to `mysql_init'
mysql.cpp:(.text+0xa66): undefined reference to `mysql_real_connect'
mysql.cpp:(.text+0xa91): undefined reference to `mysql_error'
/tmp/ccaIe2WK.o: In function `MySQLInitCode(char*)':
mysql.cpp:(.text+0xb45): undefined reference to `mysql_init'
mysql.cpp:(.text+0xb72): undefined reference to `mysql_real_connect'
mysql.cpp:(.text+0xb8c): undefined reference to `mysql_error'
mysql.cpp:(.text+0xbc3): undefined reference to `mysql_query'
mysql.cpp:(.text+0xbd8): undefined reference to `mysql_error'
mysql.cpp:(.text+0xc0a): undefined reference to `mysql_use_result'
mysql.cpp:(.text+0xc24): undefined reference to `mysql_fetch_row'
mysql.cpp:(.text+0xc5b): undefined reference to `mysql_free_result'
mysql.cpp:(.text+0xc67): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
Posted
Updated 27-Oct-17 1:15am
v6

1 solution

You need to include the library(s) that contain the code for those functions, which are merely defined in the header files. See the LDFLAGS entries in your Makefile for the required options.

You also have the line
C++
void InitStandalone();

in your main function. Are you sure that is supposed to be there?
 
Share this answer
 
Comments
Member 13376650 25-Oct-17 14:33pm    
About the InitStandalone() function, I saw it in another tutorial that it said:

To call these routines, your code will need a predeclaration of these
routines. Just copy the routine declarations from ChatScript and put
at the start of your file, e.g.,



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);

Also it's inside the mainSystem.h too
Member 13376650 25-Oct-17 14:35pm    
About another header files that should be included, may you explain more how can I find them? I am newbie in Linux and couldn't find them !
Richard MacCutchan 26-Oct-17 4:11am    
This has nothing to do with Linux, it is purely a compile and link issue with the C language. If you do not know how to link your object modules and libraries you need to take a look at the gcc/g++ documentation.
Member 13376650 27-Oct-17 5:40am    
I asked the author and he said "only common.h" is sufficient to add above my main! And I saw inside common.h, "mainSystem.h" is included. and inside "mainSystem.h" there is "evserver.h" . and I have all of this header file beside my main!

But I don't know why I get damn "undefined reference ..." errors!
Richard MacCutchan 27-Oct-17 6:20am    
And I have explained twice that this is an issue with missing libraries, not header files. If you do not understand how linking of object modules is used to create executable programs, then you need to study the documentation for the GCC compilation system, and the ld linker.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900