Jump to content
  • Advertisement
Sign in to follow this  
Jacol Beyros

Linking C++ code with a C library

This topic is 2807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to use the Mini-XML library (http://www.minixml.org/), which is coded in C. I compiled the library using gcc (I have MinGW installed on Windows XP) and ar to link it. This is the makefile i used:


CFLAGS = -c -Wall
LDFLAGS = rcs
COMPILER = gcc
SRC_FILE_EXTS = .c
LINKER = ar

_SRCS := $(wildcard *.c)
_HEADERS = mxml.h mxml-private.h config.h
LIBS =

SRC_DIR = .
HEADER_DIR =
OBJ_DIR = obj
LIB_DIR =
INC_DIR =

OUTPUT = out/libmxml.a

SRCS = $(patsubst %,$(SRC_DIR)/%,$(_SRCS))
HEADERS = $(patsubst %,$(HEADER_DIR)/%,$(_HEADERS))
OBJS = $(patsubst %,$(OBJ_DIR)/%,$(_SRCS:$(SRC_FILE_EXTS)=.o))
#CLEANABLES = $(subst /,\, $(OBJS)) $(subst /,\, $(OUTPUT))

all : $(SRCS) $(OUTPUT)

$(OUTPUT) : $(OBJS)
$(LINKER) $(LDFLAGS) $@ $<

$(OBJ_DIR)/%.o : $(SRC_DIR)/%$(SRC_FILE_EXTS)
$(COMPILER) $(CFLAGS) $< -o $@

.PHONY : clean

clean :
rm -f $(OBJS) $(OUTPUT)

and the command I used was:
mingw32-make all
It compiled fine with no errors; a warning though, which was:
mxml-doc.c:932:1: warning: 'remove_directory' defined but not used
Doesn't really seem a big deal.

So now I have a static library named 'libmxml.a'.

The source file for my program is:

//main.cpp
#include <cstdio>
#include "mxml.h"
#include <fcntl.h>

int main()
{
FILE *fp;
mxml_node_t *xml; /* <?xml ... ?> */
mxml_node_t *data; /* <data> */
mxml_node_t *node; /* <node> */

mxml_node_t *group; /* <group> */
xml = mxmlNewXML("1.0");
data = mxmlNewElement(xml, "data");

node = mxmlNewElement(data, "node");
mxmlNewText(node, 0, "val1");
node = mxmlNewElement(data, "node");
mxmlNewText(node, 0, "val2");
node = mxmlNewElement(data, "node");
mxmlNewText(node, 0, "val3");
group = mxmlNewElement(data, "group");

node = mxmlNewElement(group, "node");
mxmlNewText(node, 0, "val4");
node = mxmlNewElement(group, "node");
mxmlNewText(node, 0, "val5");
node = mxmlNewElement(group, "node");
mxmlNewText(node, 0, "val6");

node = mxmlNewElement(data, "node");
mxmlNewText(node, 0, "val7");
node = mxmlNewElement(data, "node");
mxmlNewText(node, 0, "val8");
fp = fopen("filename.xml", "w");
mxmlSaveFile(xml, fp, MXML_NO_CALLBACK);
fclose(fp);
return(0);
}

which i found on the internet somewhere; probably at the minixml site. I am using this makefile to build this source file and link it to libmxml:

CFLAGS = -c -Wall -I$(INC_DIR)
LDFLAGS = -static
COMPILER = g++
SRC_EXT = .cpp
LINKER = g++

SRCS = main.cpp
HEADERS = mxml.h
LIBS = -lmxml

SRC_DIR = .
OBJ_DIR = obj
LIB_DIR = C:\Installed_Libs\lib
INC_DIR = C:\Installed_Libs\include

OUTPUT = bin/Out.exe

_SRCS = $(patsubst %,$(SRC_DIR)/%,$(SRCS))
_HEADERS = $(patsubst %,$(INC_DIR)/%,$(HEADERS))
OBJS = $(patsubst %,$(OBJ_DIR)/%,$(SRCS:$(SRC_EXT)=.o))

all : $(_SRCS) $(OUTPUT)

$(OUTPUT) : $(OBJS)
$(LINKER) $(LDFLAGS) $< -L$(LIB_DIR) $(LIBS) -o $@

$(OBJ_DIR)/%.o : $(SRC_DIR)/%$(SRC_EXT)
$(COMPILER) $(CFLAGS) $< -o $@

.PHONY : clean

clean :
rm -f $(OBJS) $(OUTPUT)

libmxml.a is in the specified lib folder: $(LIB_DIR) or 'C:\Installed_Libs\lib' and mxml.h is in the specified include folder: $(INC_DIR) or 'C:\Installed_Libs\include'.

Main.cpp compiles fine, but when it gets linked:
g++ -static obj/main.o -LC:\Installed_Libs\lib -lmxml -o bin/Out.exe
I get multiple undefined reference errors to every mxmlNewXML, mxmlNewElement, mxmlNewText, and mxmlSaveFile function in main.cpp.

I cannot figure out what is going wrong here. I have tried to compile the library using g++, but that ends in more errors than ever; I've tried compiling the library with 'g++ -x c ...' but that wasn't any different than with gcc; I've tried linking the library's object files with the main.o from the main.cpp; each and every attempt ends with those same undefined references. The linker is definitely finding the library file (libmxml.a) and the library's header file, mxml.h, because I've removed or renamed them and got errors complaining about that.

So, if you've read this far, I already thank you. If you understand what I've said, and have an idea on why it's failing so badly, I would love to hear about it. Again, thank you.

Share this post


Link to post
Share on other sites
Advertisement
Does the Mini-XML library header wrap it's functions in extern "C" if it detects it's being compiled as C++?

Share this post


Link to post
Share on other sites
Dang it. I tried to remember to put that in the first post. Yes, it is wrapped in extern "C".

Edit: I made a project in CodeBlocks and set it all up properly, libmxml.a in the project's build options, and the lib folder and include folder under "Search Directories." I got the same errors. This was the latest CodeBlocks with MinGW included.

[Edited by - Jacol Beyros on October 15, 2010 7:03:17 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!