1
votes

I am new to c++ and I have the following problem:

files: - main.cpp - utils.h - utils.cpp

When I am doing:

g++ -c -std=c++11 utils.cpp (compiles) g++ -c -std=c++11 main.cpp (compiles)

when I try to link:

g++ -o main.o utils.o

/usr/lib/gcc/i686-redhat-linux/4.8.3/../../../crt1.o: In function _start': (.text+0x18): undefined reference tomain' utils.o: In function clean_html(std::string const&)': utils.cpp:(.text+0xfa): undefined reference totidyCreate' utils.cpp:(.text+0x118): undefined reference to tidyOptSetBool' utils.cpp:(.text+0x13b): undefined reference totidyOptSetBool' utils.cpp:(.text+0x15e): undefined reference to tidyOptSetBool' utils.cpp:(.text+0x181): undefined reference totidyOptSetBool' utils.cpp:(.text+0x1a4): undefined reference to tidyOptSetBool' utils.cpp:(.text+0x1c7): undefined reference totidyOptSetValue' utils.cpp:(.text+0x1ea): undefined reference to tidyOptSetValue' utils.cpp:(.text+0x209): undefined reference totidyOptSetBool' utils.cpp:(.text+0x228): undefined reference to tidyOptSetBool' utils.cpp:(.text+0x247): undefined reference totidyOptSetInt' utils.cpp:(.text+0x281): undefined reference to tidyParseString' utils.cpp:(.text+0x295): undefined reference totidyCleanAndRepair' utils.cpp:(.text+0x2b0): undefined reference to tidySaveBuffer' utils.cpp:(.text+0x322): undefined reference totidyBufFree' utils.cpp:(.text+0x32d): undefined reference to `tidyRelease' collect2: error: ld returned 1 exit status

In utils.cpp I have a function clean_html. When I remove this function the code is linked with success.

I am using gcc version 4.8.3 20140911 (Red Hat 4.8.3-7) (GCC) .

Tidy is installed via fedora repos using: Package libtidy-devel-0.99.0-28.20091203.fc19.i686 already installed and latest version Package libtidy-0.99.0-28.20091203.fc19.i686 already installed and latest version

Edit:

Forgot to mention: - I include tidy.h using #include

tidy.h is at /usr/include/tidy.h

1
Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it?. You probably missed to specify -ltidy on linking stage.πάντα ῥεῖ
can you show the link command. You're likely missing the -l option to link the tidy library.geert3
@geert3 I addded the link command. What is the library name to add to -l? And how do I figure this out?gosom
Ok I fifure this out. Thanks @geert3. I had to add -ltidy to the link command.If you want add an answer to acceptgosom
It is likely /usr/lib/libtidy.so, so you would need g++ -ltidy -o main.o utils.o.geert3

1 Answers

4
votes

You need to add -ltidy to your link command.