linking of C library in C++ program

Hello,

I have a C++ program, which calls a function (dsyeig(...)) from a C-library (libmrrr.a). The code for the library consists of a number of files. However, the function I am calling is situated in mrrr.h.

So, to call it, I have used extern "C"{}, where I include the library:

1
2
3
extern "C"{
#include "./mr3smp-master/INCLUDE/mrrr.h"
}


To link the library, I have created a makefile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

DIRS = ./ ./m3smp-master/INCLUDE ./m3smp-master/INCLUDE/SRC


# Source files
HEADERS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.h))
CSRCS   := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.cpp))
COBJS = $(CSRCS:.cpp=.o)


# Build target #
RPApolar: $(COBJS) $(HEADERS)
	   g++ -o RPApolar $(COBJS) $(HEADERS) libmrrr.a -lpthread -static -L./libmrrr

.PHONY: clean
clean:
	rm -f *~ core.* *__genmod* \
        ./INSTALL/*~ \
        $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.o)) \
        $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*~)) \
        $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.mod.*)) \
        $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*__genmod*)) */


When building the program RPApolar, the libmrrr is found and seems to be correctly linked. However, I get many linker errors as this one:

libmrrr.a(dense.o): In function 'dsyeig': /home/magdyn/.../SRC/dense.c:211: undefined reference to 'dsytrd_'

So, it seems, as if the linker cannot link correctly to the file dense.h, which is called by the library-internal function dsyeig. What do I have to change in my make file to have the linker linking correctly?

Thanks in advance for any advice.

Let's start from the beginning, why are you modifying the original authors makefile found here: https://github.com/HPAC/mr3smp/blob/master/Makefile ?

You're missing the '-I' CPFLAG flag that tells the compiler to search a certain directory when looking for header files to include. I think it should go on Line 13 before your "$(Headers)" variable but I don't normally compile code this way.
I have used the original makefile to create the library. The makefile here is for my C++ program, which uses the library
Then why are you recompiling the library? Just link to it and include their headers in your project.
The -I CXXFLAG does not affect the linker problem. I have just tried it, but still thanks.
Right, I thought you were compiling the library. But if you've already done that then you can skip all of that and read my previous post again.
Thank you very much. I have no experience with makefiles and compiling with g++ and while trying to solve the problem the makefile might have gotten a bit out of control. I do not see, where I recompile the library. However, I have cut the makefile down to the following form:

1
2
3
4
5
6
7
8
9
10
11
12
13

DIRS = ./ 


# Source files
HEADERS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.h))
CSRCS   := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.cpp))
COBJS = $(CSRCS:.cpp=.o)


# Build target #
RPApolar: $(COBJS) $(HEADERS)
	   g++ -o RPApolar $(COBJS) -I./m3smp-master/INCLUDE $(HEADERS) libmrrr.a -lpthread -static -L./libmrrr */


The only file in ./ are main.cpp and one additional header and cpp file of a class. The error message is

g++ -o RPApolar .//vwwp.o .//main.o -I./m3smp-master/INCLUDE .//vwwp.h libmrrr.a                                                                                                              -lpthread -static -L./libmrrr
libmrrr.a(dense.o): In function `dscale_matrix':
/home/.../mr3smp-master/SRC/dense.c:495: undefined reference to `dlan                                                                                                             sy_'
libmrrr.a(dense.o): In function `dsyeig':
/home/.../mr3smp-master/SRC/dense.c:202: undefined reference to `dsyt                                                                                                             rd_'
/home/.../mr3smp-master/SRC/dense.c:211: undefined reference to `dorm                                                                                                             tr_'......


A LOT MORE OF THAT


collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'RPApolar' failed
make: *** [RPApolar] Error 1


If I do recompile the library, I do not see, where.
The error you posted listed a source code file from the library, "dense.c". This tells me that you're trying to compile the source code from this library into your project. Stop that. Once the library is built, it's a stand alone entity. You only need the header files so that the compiler will know what things look like. Feel free to copy and past those into a separate folder for your project if you think it will make things easier.
Thanks again. I am not recompiling the library (all of the source files are in a place, which is not in DIRS). However, I have found out something: the respective functions which cannot be linked, are parts of the library, which are written in Fortran.

My new makefile looks as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14


DIRS = ./ ./mr3smp-master/INCLUDE


# Source files
HEADERS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.h))
CXXSRCS   := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.cpp))
CXXOBJS = $(CSRCS:.cpp=.o)


# Build target #
RPApolar: $(CXXOBJS) $(HEADERS)
	   g++ -o RPApolar $(CXXSRCS) -I./m3smp-master/INCLUDE $(HEADERS) libmrrr.a -lpthread -static -L./libmrrr -lgfortran */


but I still get the error messages. Does anyone know, how to solve this problem?
Topic archived. No new replies allowed.