reset password
Author Message
se1k1h1mawar1
Posts: 121
Posted 13:08 Jan 15, 2015 |

After copying all the source files (i.e. util.c, utility.h, Sample1.cpp etc) to my mars.calstatela.edu account via ssh, I executed the command to create an object file (util.o) off of util.c. It seemed to have compiled the util.c file fine, and the object file was created successfuly since I did not get any error message at this point.
However, when I tried to compile sample1.cpp with g++ command, I got the error message listed below:

    Undefined                       first referenced
     symbol                             in file
    clean_up_shared                     /var/tmp//ccJNC5bS.o
    process_fork                        /var/tmp//ccJNC5bS.o
    process_join                        /var/tmp//ccJNC5bS.o
    shareint                            /var/tmp//ccJNC5bS.o
    ld: fatal: Symbol referencing errors. No output written to cpp/sample1
    collect2: ld returned 1 exit status​

​ By reading the error message, I understood that the OS looks for the functions and variables defined in util.c such as process_fork, process_join in ccJNC5bS.o in "tmp" directory inside "var"​ instead of the actual location in which I have my object file (util.o). Am I correct in interpretating this error message?

Could anybody please suggest how I can link the location of which the OS will look for these functions and variables ( clearly invisible to it currently)?

I hope my understanding of the materials is not too off.
Any insight would be greatly appreciated.
Thank you,

Last edited by se1k1h1mawar1 at 13:10 Jan 15, 2015.
gjmiles
Posts: 17
Posted 19:07 Jan 15, 2015 |

What command arguments did you use with g++?

The text example was something like:

g++ -o foo foo.cpp /path/to/util.o

se1k1h1mawar1
Posts: 121
Posted 19:23 Jan 15, 2015 |

The command I used to compile util.c and to create the object file:

gcc -c util.c -o util.o

The commands I tried to compile sample1.cpp:

g++ sample1.cpp sample1
g++ sample1.cpp -o sample1
g++ sample1.cpp

In case it clarifies my question better, I will list the outputs I got in response to the above commands.

---- Commad -----> gcc -c util.c -o util.o

---- Commad -----> g++ sample1.cpp sample1
g++: sample1: No such file or directory

---- Commad -----> g++ sample1.cpp -o sample1
Undefined                       first referenced
 symbol                             in file
clean_up_shared                     /var/tmp//ccFwA0Re.o
process_fork                        /var/tmp//ccFwA0Re.o
process_join                        /var/tmp//ccFwA0Re.o
shareint                            /var/tmp//ccFwA0Re.o
ld: fatal: Symbol referencing errors. No output written to sample1
collect2: ld returned 1 exit status

---- Commad -----> ls
lab1_2                          src
lab1_2_rock_paper_scissors.cpp  testmb.cpp
mail                            testsb.cpp
mbarrier.h                      util.c
sample1.cpp                     util.o
sbarrier.h                      utility.h

---- Commad -----> g++ sample1.cpp
Undefined                       first referenced
 symbol                             in file
clean_up_shared                     /var/tmp//cc91IzKc.o
process_fork                        /var/tmp//cc91IzKc.o
process_join                        /var/tmp//cc91IzKc.o
shareint                            /var/tmp//cc91IzKc.o
ld: fatal: Symbol referencing errors. No output written to a.out

 

Thank you,

gjmiles
Posts: 17
Posted 20:24 Jan 15, 2015 |

You have to include the util.o library in g++ arguments when you compile a program that is linking to it. 

If util.o is in the same directory as your source code file then you do not need to specify a path.

i.e.:

g++ -o sample sample1.cpp util.o
 

se1k1h1mawar1
Posts: 121
Posted 20:27 Jan 15, 2015 |

Oh my goodness, I cannot believe I missed that...
Thank you very much, it worked!

 

Last edited by se1k1h1mawar1 at 20:29 Jan 15, 2015.
kevinmowers@yahoo.com
Posts: 49
Posted 14:58 Jan 20, 2015 |

I'm getting these kind of errors when I try to compile util.c

util.c:1: error: syntax error before '<' token
util.c:18:10: #include expects "FILENAME" or <FILENAME>
util.c:19:10: #include expects "FILENAME" or <FILENAME>
util.c: In function `process_join':
util.c:52: error: `lt' undeclared (first use in this function)
util.c:52: error: (Each undeclared identifier is reported only once
util.c:52: error: for each function it appears in.)
util.c:52: error: syntax error before '=' token
util.c:52: error: syntax error before ')' token

what am I doing wrong?