I am a beginner Fortran programmer. I am having trouble linking a static library to a Fortran program. In the module rce_world.f90, I call a function that is defined in the library libcamclouds.a. I get an error suggesting the function is undefined.
I don't think I am linking the library correctly in my Makefile, AND/OR there is some external call in the actual Fortran module that I am missing (we don't need to supply a "use " in the module?). Any help would be greatly appreciated.
My Makefile is:
#Defining Variables
objects = homme_lat_lon.o cam_grid.o cam_variables.o rce_world.o initial.o
FC = gfortran
LIBDIR = -L/glade/p/work/aherring/RCE_world_initial/no_SICTHK/moistplume/cam5wv/cfcode_for_adam/
LIBNAM = -lcamclouds
#Makefile
out_data: $(objects)
$(FC) -g -o out_data $(objects) $(LIBDIR) $(LIBNAM) -I/sw/include
homme_lat_lon.mod: homme_lat_lon.o homme_lat_lon.f90
$(FC) -g -c homme_lat_lon.f90
homme_lat_lon.o: homme_lat_lon.f90
$(FC) -g -c homme_lat_lon.f90
cam_grid.mod: cam_grid.o cam_grid.f90
$(FC) -g -c cam_grid.f90
cam_grid.o: cam_grid.f90
$(FC) -g -c cam_grid.f90
cam_variables.mod: cam_variables.o cam_variables.f90
$(FC) -g -c cam_variables.f90
cam_variables.o: cam_variables.f90
$(FC) -g -c cam_variables.f90
rce_world.mod: cam_grid.mod cam_variables.mod rce_world.o rce_world.f90
$(FC) -g -c rce_world.f90
rce_world.o: cam_grid.mod cam_variables.mod rce_world.f90
$(FC) -g -c rce_world.f90
initial.o: homme_lat_lon.mod cam_grid.mod cam_variables.mod rce_world.mod initial.f90
$(FC) -g -c initial.f90
Error log:
rce_world.o: In function __rce_world_MOD_rce_initial': /glade/p/work/aherring/RCE_world_initial/no_SICTHK/moistplume/cam5wv/cfcode_for_
adam/rce_world/rce_world.f90:168: undefined reference to aqsat_' collect2: ld returned 1 exit status make: *** [out_data] Error £
Related
After reading lots of post I am really confused.
I want to link a dynamic library to my cpp code.
The library is in /usr/local/include/sbml
and the libsbml.so file can be found in /usr/local/lib
I have a makefile that looks like this
SHELL = /bin/sh
VERSION = 5.04.02
CC = /usr/bin/g++
CFLAGS = -Wall -g -D_REENTRANT -DVERSION=\"$(VERSION)\"
LDFLAGS = -lsbml
OBJ = main.o SBML.o
prog: $(OBJ)
$(CC) $(CFLAGS) -o prog $(OBJ) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $<
.PHONY : clean
clean :
-rm edit $(OBJ)
If I run the makefile I get the following error: (undefined reference to SBMLReader::readSBMLFromFile())
g++ -c -o SBML.o SBML.cpp
/usr/bin/g++ -Wall -g -D_REENTRANT -DVERSION=\"5.04.02\" -o prog main.o SBML.o -lsbml
sbml.o: In Funktion `SBML::readSBML()':
sbml.cpp:(.text+0x129): Nicht definierter Verweis auf `SBMLReader::readSBMLFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
The library was not linked because you need to specify the folder containing the so-file as well:
LDFLAGS = -L/usr/local/lib -lsbml
Now it should link and your unresolved symbol should be gone as well.
After try to use this attached makefile the error that appears is :
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Ass1F] Error 1
the makefile is :
all: Ass1F
Ass1F: bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
g++ -o bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
#echo 'Finished building target: Ass1F'
#echo ' '
bin/x.o: src/x.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/x.o src/x.cpp
bin/y.o: src/y.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/y.o src/y.cpp
bin/z.o: src/z.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/z.o src/z.cpp
bin/w.o: src/w.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/w.o src/w.cpp
bin/main.o: src/main.cpp
g++ -g -Wall -Weffc++ -c -Linclude -o bin/main.o src/main.cpp
clean:
rm -f bin/*
what should I do with this problem?
the reason is the makefile or something in the code?
just to let you know we used eclipse to write the code and everything work perfectly- no any bugs.
thanks
Line g++ -o bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o tries to create executable named bin/main.o, overwriting its previous contents.
It should be e.g. g++ -o Ass1F bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
Your makefile recipe for Ass1F is incorrect, it doesn't specify the output file, os make uses bin/main.o as the output file instead of one of the input files.
You can simplify the makefile by using pattern rules (i.e. with wildcards) and using pre-defined variables such as $# (the name of the target) and $^ (the list of prerequisites) and CXX (the C++ compiler):
CXXFLAGS = -g -Wall -Weffc++
CPPFLAGS =
LDFLAGS =
LDLIBS =
all: Ass1F
Ass1F: bin/main.o bin/x.o bin/y.o bin/z.o bin/w.o
$(CXX) $(LDFLAGS) -o $# $^ $(LDLIBS)
#echo 'Finished building target: Ass1F'
#echo ' '
bin/%.o: src/%.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $# $^
clean:
rm -f bin/*
# Compiler
CXX := g++
CF := -g
# c files
CFILES := $(wildcard src/*.cpp)
SOURCES := $(CFILES) # $(CFILES2) all CFILES
# o files
OBJECTS := $(SOURCES:.cpp=.o)
OBJECTS_A:= $(SOURCES:.cpp=_a.o)
# Macros
MACRO = -D
INFO = -DINFO // optional
DCDREAD = -DDCDREAD
# More Macros
DEFAULT = -DDEFAULT
CONTACT = -DDEFAULT -DCONTACT -DCONTACTPERSIST -DTENSION_COSTHETA
MTCONTACT = -DDEFAULT -DMTCON1 -DMTCON2
DCDCON = -DDCDREAD -DCONTACT -DCONTACTPERSIST -DTENSION_COSTHETA
DCDMT = -DDCDREAD -DMTCON1 -DMTCON2
CENMOV = -DDCDREAD -DMTCON1 -DCENTROIDMOVEMENT
ANGLE = -DDCDREAD -DINERTIA -DANGLE
ANG3 = -DDCDREAD -DMTCON1 -DANGLE3CENTROID
ang3o: $(OBJECTS_A)
$(CXX) $(OBJECTS_A) -o test/$(EXEC)_dcd_angle3centroid
cd test && ./$(EXEC)_dcd_angle3centroid mtonly_seamup.ref.pdb mtonly_seamup_d1_indent.dcd 1 209 1
ang3: $(OBJECTS)
$(CXX) $(SOURCES) $(CF) $(INC) $(LIB) $(ANG3) -o test/$(EXEC)_dcd_angle3centroid
cd test && ./$(EXEC)_dcd_angle3centroid mtonly_seamup.ref.pdb mtonly_seamup_d1_indent.dcd 1 209 1
# To obtain object files
$(OBJECTS_A) : $(SOURCES)
$(CXX) $(CF) $(INC) $(LIB) $(ANG3) -c $< -o $#
%.o: %.cpp
$(CXX) $(CC_FLAGS) $(INC) $(LIB) -c $< -o $#
I know the objects are unnecessary in the ANG3 rule. The c files are compiled directly, the executable runs correctly! I'd like to switch to objects, an ANG3O rule. HERE is my main question, am I trying to link .o files incorrectly at the end of the ANG3O rule?
ANG3 works.
ANG3O fails. Multiple Definitions of Function Error.
make ang3
g++ -g -O3 -Iinclude -pthread -c src/readfile.cpp -o src/readfile.o
g++ -g -O3 -Iinclude -pthread -c src/main.cpp -o src/main.o
g++ -g -O3 -Iinclude -pthread -c src/md.cpp -o src/md.o
g++ -g -O3 -Iinclude -pthread -c src/chain.cpp -o src/chain.o
g++ src/readfile.cpp src/main.cpp src/md.cpp src/chain.cpp -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -o test/run_segment_dcd_angle3centroid
make ang3o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/readfile_a.o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/main_a.o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/md_a.o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/chain_a.o
g++ src/readfile_a.o src/main_a.o src/md_a.o src/chain_a.o -o test/run_segment_dcd_angle3centroid
src/main_a.o: In function `ReadLines(Chain*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: multiple definition of `ReadLines(Chain*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: first defined here
src/main_a.o: In function `ReadMolecularContent(char*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: multiple definition of `ReadMolecularContent(char*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: first defined here
src/md_a.o: In function `ReadLines(Chain*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: multiple definition of `ReadLines(Chain*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: first defined here
src/md_a.o: In function `ReadMolecularContent(char*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: multiple definition of `ReadMolecularContent(char*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: first defined here
src/chain_a.o: In function `ReadLines(Chain*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: multiple definition of `ReadLines(Chain*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:43: first defined here
src/chain_a.o: In function `ReadMolecularContent(char*)':
/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: multiple definition of `ReadMolecularContent(char*)'
src/readfile_a.o:/home/d/sop_dev/contacts/segment/src/readfile.cpp:208: first defined here
/usr/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:172: recipe for target 'ang3o' failed
make: *** [ang3o] Error 1
NOTES:
Related question: Error with multiple definitions of function
Possibly related: One definition rule and different class definitions in two translation units
However, in my situation:
the headers are in the include directory. The c files are in a src directory. All functions are declared in the headers. Only headers are included, no c/cpp files. Makefile examples seem to halt just before this level of complexity, the use of different define segments of code, and the need to compile objects with/without certain defined sections.
It helps to actually read the output.
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/readfile_a.o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/main_a.o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/md_a.o
g++ -g -Iinclude -pthread -DDCDREAD -DMTCON1 -DANGLE3CENTROID -c src/readfile.cpp -o src/chain_a.o
You're compiling the same C++ source file over and over again, then linking it to itself.
Your mistake is in the recipe to build objects. I'll leave you to read the documentation and find out why.
I am developing a little program with pocketsphinx (speech to text - library).
On Windows i was using Code::Blocks as development environment and i had success to build a program.
Now i try to port my program to Linux and i am having little problems to link against pocketsphinx.
This is the Makefile:
CC = g++
CFLAGS = -Wall -std=c++11
LDFLAGS = -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx
OBJ = obj/Application.o obj/Main.o obj/Recorder.o
all: $(OBJ)
$(CC) -L/usr/local/lib -o bin/Eve $(OBJ) -s -lsphinxbase -lpocketsphinx
obj/Main.o: src/Main.cpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Main.cpp -o obj/Main.o
obj/Application.o: src/Application.cpp src/Application.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Application.cpp -o obj/Application.o
obj/Recorder.o: src/Recorder.cpp src/Recorder.hpp
$(CC) $(CFLAGS) $(LDFLAGS) -c src/Recorder.cpp -o obj/Recorder.o
It is the same which i was using on Windows, i just adjusted the file path.
I am receiving the following error:
$ make
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Application.cpp -o obj/Application.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Main.cpp -o obj/Main.o
g++ -Wall -std=c++11 -I/usr/local/include/sphinxbase -I/usr/local/include/pocketsphinx -c src/Recorder.cpp -o obj/Recorder.o
g++ -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o -s -lsphinxbase -lpocketsphinx
obj/Recorder.o: In function `Recorder::Recorder()':
Recorder.cpp:(.text+0x1c0): undefined reference to `ad_open_sps'
Recorder.cpp:(.text+0x20d): undefined reference to `ad_read'
Recorder.cpp:(.text+0x215): undefined reference to `cont_ad_init'
obj/Recorder.o: In function `Recorder::~Recorder()':
Recorder.cpp:(.text+0x2f3): undefined reference to `cont_ad_close'
Recorder.cpp:(.text+0x303): undefined reference to `ad_close'
obj/Recorder.o: In function `Recorder::recognizeFromMicrophone()':
Recorder.cpp:(.text+0x37a): undefined reference to `ad_start_rec'
Recorder.cpp:(.text+0x395): undefined reference to `cont_ad_calib'
Recorder.cpp:(.text+0x3f0): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x4e6): undefined reference to `cont_ad_read'
Recorder.cpp:(.text+0x5b5): undefined reference to `ad_stop_rec'
Recorder.cpp:(.text+0x5d8): undefined reference to `ad_read'
Recorder.cpp:(.text+0x5f4): undefined reference to `cont_ad_reset'
collect2: error: ld returned 1 exit status
make: *** [all] Fehler 1
I don't think that it is a name mangling problem, since i built the lib on my own using the provided Makefile.
What can i do to link against the lib without errors?
EDIT: I figured out how to make it work. I simply modified the rule of the target "all" to this:
$(CC) -static -L/usr/local/lib -o bin/Eve $(OBJ) -s -lpocketsphinx -lsphinxbase -lsphinxad -lpthread
Functions like ad_read are defined in libsphinxad library, you need to add it to your linker command line:
g++ -static -L/usr/local/lib -o bin/Eve obj/Application.o obj/Main.o obj/Recorder.o \
-lpocketsphinx -lsphinxbase -libsphinxad
Please note that the order of libraries is important.
I think I have a problem with my makefile. I'm writing this program:
Q2.cpp contains the main.
Agent.cpp Agent.h
Timing.cpp Timing.h
RandomDouble.cpp RandomDouble.cpp
And I'm using the header randoma.h in RandomDouble.cpp.
I downloaded the randomaelf64.a file and I wrote this makefile:
Q2 : Q2.o Agent.o Timing.o RandomDouble.o
g++ -Wall -g randomaelf64.a RandomDouble.o Q2.o Agent.o Timing.o -o Q2
Q2.o : Q2.cpp Agent.h Timing.h
g++ -Wall -g -c Q2.cpp -o Q2.o
Agent.o : Agent.cpp Agent.h Timing.h RandomDouble.h PrintQ2.h
g++ -Wall -g -c Agent.cpp -o Agent.o
RandomDouble.o : RandomDouble.cpp RandomDouble.h randoma.h
g++ -Wall -g -c RandomDouble.cpp -o RandomDouble.o
Timing.o : Timing.cpp Timing.h Agent.h
g++ -Wall -g -c Timing.cpp -o Timing.o
clear :
rm *.o Q2
Except for the first command, each g++.. command is working when I do it separately.
Even when I add a main() to RandomDouble.cpp and run:
g++ -Wall -g randomael64.a RandomDouble.cpp -o rand
it's working. So I think that maybe the problem is with my makefile.
When I run make I get this error:
RandomDouble.o: In function `InitSeed()':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:11: undefined reference to `MersenneRandomInit'
RandomDouble.o: In function `InitSeed(int)':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:16: undefined reference to `MersenneRandomInit'
RandomDouble.o: In function `GetRandomDouble()':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:21: undefined reference to `MersenneRandom'
Agent.o: In function `Agent::SendMessage()':
/cs/stud/ofrenk33/CPP/ex1/Agent.cpp:31: undefined reference to
...
make: *** [Q2] Error 1
I need to say that there are functions declared in randoma.h which are in the randomaelf64.a library.
How do I fix this problem?
You need to link against the library after all your own object files. The linker will only include as much of the library as it needs, and if there are no unresolved references yet, then none of the library will be needed. Change the first rule to:
Q2 : Q2.o Agent.o Timing.o RandomDouble.o
g++ -Wall -g RandomDouble.o Q2.o Agent.o Timing.o randomaelf64.a -o Q2