我无法在使用 g++ 编译的程序中链接使用 gcc 编译的共享库。我有一个使用以下命令通过 gcc 编译的共享库:
build-library: activate-library-mode activate-debug-mode build-headers build-c-files build-exe-files
@echo -e $(cyan)generating shared library...$(plain)
@for exefile in $(exefiles); do\
echo "$(CC) $(DEBUG) $(EXELIBFLAG) -o $(BINDIR)lib`basename $$exefile .c`.so $(wildcard $(OBJDIR)*.o)";\
$(CC) $(DEBUG) $(EXELIBFLAG) -o $(BINDIR)lib`basename $$exefile .c`.so $(wildcard $(OBJDIR)*.o);\
done
在哪里:
- 抄送=GCC
- 调试=-g
- EXELIBFLAG=-共享
- BINDIR=bin/
- exefiles=src/main.c
- *.o 文件使用 -fPIC、-g、-Wall 一些 -I 和 -c 选项编译
因此,结果调用是:
gcc -g -shared -o bin/libmain.so obj/ClassElement.o obj/ClassHashTable.o obj/ClassHTCell.o obj/IdentifierList.o obj/LabelClassType.o obj/LabelHashTable.o obj/LabelHTCell.o obj/Label.o obj/lexer.o obj/lex-tools.o obj/LexVal.o obj/LocalResourceElement.o obj/LocalResourceHashTable.o obj/LocalResourceHTCell.o obj/main.o obj/main-tools.o obj/memory-tools.o obj/NodeType.o obj/parser.o obj/parser-tools.o obj/PassMode.o obj/ResourceClassType.o obj/ResourceElement.o obj/ResourceHashTable.o obj/ResourceHTCell.o obj/schemaClassType.o obj/schema.o obj/semantic.o obj/SyntaxNode.o
我在 LD_LIBRARY_PATH 中添加了共享库的路径,仅用于测试目的:
LD_LIBRARY_PATH=/home/koldar/Documents/git/Kaboom/custom-object-language/CustomProgrammingLanguage/bin:
export LD_LIBRARY_PATH
然后该库由以下程序使用:
#include "Label.h"
int main(){
Plabel l=initLabel("hello",LABEL_PACKAGE); //use library function
printf("OK.\n");
return 0;
}
这个程序是用g++通过命令编译的:
g++ -I /home/koldar/Documents/git/Kaboom/custom-object-language/CustomProgrammingLanguage/include/ -I /home/koldar/Documents/git/Kaboom/custom-object-language/KaboomTest/cute/ -L /media/Dati/Users/Koldar/Documents/git/Kaboom/custom-object-language/CustomProgrammingLanguage/bin -l main test.c -o "KaboomTest"
问题是 g++ 打印出错误:
test.c: In function ‘int main()’:
test.c:4: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccF7XCBs.o: In function `main':
test.c:(.text+0x19): undefined reference to `initLabel(char*, LabelClassType)'
collect2: ld returned 1 exit status
真正的问题是,如果我用 gcc 编译 test.c,程序就可以正常工作:
gcc -I /home/koldar/Documents/git/Kaboom/custom-object-language/CustomProgrammingLanguage/include/ -I /home/koldar/Documents/git/Kaboom/custom-object-language/KaboomTest/cute/ -L /media/Dati/Users/Koldar/Documents/git/Kaboom/custom-object-language/CustomProgrammingLanguage/bin -l main test.c -o "KaboomTest"
koldar@Octav:~/Documents/git/Kaboom/custom-object-language/KaboomTest$ ./KaboomTest
OK.
koldar@Octav:~/Documents/git/Kaboom/custom-object-language/KaboomTest$
现在我很确定我可以在 g++ 编译程序中链接 gcc 编译的共享库,但我该怎么做呢?感谢您的回答,对不起我的英语不好