2

I'm struggling to link a project to an external project (named Prog). I'm not used with big project and dependecies problem, It's the first time I encounter such problem to link another library.

I want to include this library as a static or better, as a dynamic library to MyProject. The compilation of Prog gives me static libraries (".a"), I included them to my "basic" Makefile, here is a picture of it :

Makefile :

CXX = g++
SRCS = $(shell find . -name "*.cpp")
OBJS = $(addsuffix .o, $(basename $(SRCS))
EXEC = test
CXXFLAGS += -std=c++17 -O3 

LIBS = -L/usr/include/lib -lprogvcore \
       -lpthread -lz -lm


CXXFLAGS += -I/usr/local/include/code \
            -std=c++11 -O3


all : $(EXEC)

$(EXEC): $(OBJS) 
    @$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)


%.o: %.cpp 
    @$(CXX) -o $@ -c $< $(CXXFLAGS) $(LIBS)

.PHONY: clean mrproper

clean:
        @rm -f $(OBJS)

mrproper: clean
        @rm -rf $(EXEC)

Here is a picture of the project hierarchy :

MyProject
|
|
|------ TESTMain.cpp
|
|------ TESTFILES_THAT_USE_PROG_FUNCTIONS
|                    |
|                    |------ MC 
|                    |             |
|                    |             |------- PROG.cpp
|                    |             |------- PROG.h 
|
|------ PROG-Project
|
|------ Makefile

When I run, the compiler recognizes the headers of PROG files, but when I want to use PROG functions it returns :

MC/PROG.o : In function « PROG::createModel(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) » :
/home/git/test/test-src/MC/PROG.cpp:37 : underfined reference to « PROGCore_init_data() »
/home/git/test/test-src/MC/PROG.cpp:41 : underfined reference to « PROGCore_init(PROGEnv_TAG*, void (* (*) [2])(PROGEnv_TAG*), int) »
collect2: error: ld returned 1 exit status
Makefile:46: recipe for target 'test' failed
make: *** [test] Error 1

I tried to generate dynamic library to avoid the problem of ordering the libraries, but didn't work.

Excuse my english, but I hope my problem was clear.

Thank you.

helppls
  • 21
  • 2

2 Answers2

0

Undefined reference indicates the linker is unable to find the library, look for libnusmvcore in ./nusmv/NuSMV/build. Be careful with the relative path, maybe change that to an absolute path to make sure linker is looking in the right spot.

Dom G
  • 56
  • 2
  • I used absolute paths (i put All needed libraries in /user/local/lib) but didnt work either. It seemes that my project does not use the static libnusmv library. – helppls Feb 03 '20 at 19:30
0

LIBS variable is wrong, it should be LDFLAGS Second you are passing the -static flag everywhere which makes no sense are you trying to build a static library if not I suggest removing it as that is not an instruction as written to the linker as written. Also you don't do linking when compiling .o's from .cpp's

also strongly suggest when defining SRC and OBJS you use ':=' instead of '=' so that these are defined once and not reevaluated each time you mention them.

UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
  • I thought that including libnusmv.a in LDFLAGS like you correct me, means i linked lnusmv to MyProject, did I miss anything ? – helppls Feb 03 '20 at 19:36
  • you only provide linker flags to the linker step, `$(EXEC): $(OBJS) @$(CXX) -o $@ $^ $(CXXFLAGS) $(LIBS)` as you had it. call it what you like but generally one calls this LDFLAGS to match conventions the compiler step does not link; so linker flags have no meaning there as you are compiling a binary and not a library so the `--static` doesn't seem to me to have any meaning at all. you need to be clear about which things you are building and which you have – UpAndAdam Feb 03 '20 at 19:59
  • I want to compile my project (named BMCTool) with an external project (named NuSMV). This latter generates static libraries (*.a) that I add in my Makefile to link it with my project. The problem is when I compile the error above appears and its like my project doesn't use the mentioned libraries of NuSMV. – helppls Feb 06 '20 at 16:11
  • Compile with has no meaning. You need to be clear and precise. Are you trying to build BMCTool which needs to be linked with the library NuSMV produces? Everything you said afterwards is gibberish frankly and nonsensical. One thing at a time :-) – UpAndAdam Feb 10 '20 at 22:48