Почему я получаю ошибки "неопределенная ссылка на...", когда включаю библиотеку?
Пожалуйста, посмотрите на этот github, который объясняет, как использовать ChatScript в качестве библиотеки внутри программы C/C++ (см. Только встраивание шагов 1 и 2): [^]
Как говорится в учебнике, я должен сделать 2 звонка.
1. InitSystem() вызов функции инициализации библиотеки.
2-функция performChat() для передачи строки в библиотеку и получения выходной строки в качестве ответа.
Что я уже пробовал:
Поэтому я написал этот простой код на C++ :
#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; }
И я знаю, что обе упомянутые функции находятся внутри mainSystem.cpp, поэтому я включил этот файл в свою программу.(это mainSystem.h : https://github.com/bwilcox-1234/ChatScript/blob/master/SRC/mainSystem.h а вот это mainSystem.cpp https://github.com/bwilcox-1234/ChatScript/blob/master/SRC/mainSystem.cpp)
Все выглядит нормально, но когда я пытаюсь скомпилировать свой код с помощью
g++ -o mayprogram main.oтам написано::
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
Это очень странно для меня!!! Почему я получаю это сообщение об ошибке, когда включаю mainSystem.h над своим кодом?
***о main.o я должен сказать, что построил его, запустив
make standaloneкомандование раньше, с моим
main.cppуспешно!
Это сделает файл:
.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: когда я попробую эту команду:
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
Это дает мне такой результат:
^ /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