Koldar
Koldar

Reputation: 1407

Use C shared library in C++ program

I have trouble to link a shared library compiled with gcc in a program compiled with g++. I have a shared library compiled through gcc using the following command:

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

where:

So, the resulting calling is:

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

I added the path of the shared library in LD_LIBRARY_PATH for testing purposes only:

LD_LIBRARY_PATH=/home/koldar/Documents/git/Kaboom/custom-object-language/CustomProgrammingLanguage/bin:
export LD_LIBRARY_PATH

The library is then used by the following program:

#include "Label.h"

int main(){
    Plabel l=initLabel("hello",LABEL_PACKAGE); //use library function
    printf("OK.\n");
    return 0;
}

This program is compiled with g++ through the command:

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"

the problem is g++ prints out an error:

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

The real problem is that if I compiled test.c with gcc the program works just fine:

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$ 

Now I'm pretty sure I can link a gcc compiled shared library in a g++ compiled program, but how can I do it? Thanks for the answers and sorry for my bad English

Upvotes: 2

Views: 2119

Answers (1)

muteryx
muteryx

Reputation: 116

extern "C"
{
#include "Label.h"
}

Like this.

Upvotes: 7

Related Questions