0

I'd like to make a python-like dynamic integer class in C++ as an experiment. It requires me to change many integers to string types. As in here: https://www.zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html it states that fmt format_int will be best for that kind of job.

So I installed fmt with command sudo apt-get install libfmt-dev

I added a header #include <fmt/format.h>

Used it in a simple test main

int main()
{
    std::cout<<fmt::format_int(124236695253045068).str();
}

And tried to compile with g++ -lfmt -std=c++17 dynamic\ mem.cc

Output of the compiler is as follows:

/usr/bin/ld: /tmp/ccLBcSNm.o: in function `fmt::v6::format_int::format_decimal(unsigned long long)':
dynamic mem.cc:(.text._ZN3fmt2v610format_int14format_decimalEy[_ZN3fmt2v610format_int14format_decimalEy]+0x94): undefined reference to `fmt::v6::internal::basic_data<void>::digits'
/usr/bin/ld: dynamic mem.cc:(.text._ZN3fmt2v610format_int14format_decimalEy[_ZN3fmt2v610format_int14format_decimalEy]+0xad): undefined reference to `fmt::v6::internal::basic_data<void>::digits'
/usr/bin/ld: dynamic mem.cc:(.text._ZN3fmt2v610format_int14format_decimalEy[_ZN3fmt2v610format_int14format_decimalEy]+0xfa): undefined reference to `fmt::v6::internal::basic_data<void>::digits'
/usr/bin/ld: dynamic mem.cc:(.text._ZN3fmt2v610format_int14format_decimalEy[_ZN3fmt2v610format_int14format_decimalEy]+0x113): undefined reference to `fmt::v6::internal::basic_data<void>::digits'
/usr/bin/ld: /tmp/ccLBcSNm.o: in function `std::make_unsigned<long>::type fmt::v6::internal::to_unsigned<long>(long)':
dynamic mem.cc:(.text._ZN3fmt2v68internal11to_unsignedIlEENSt13make_unsignedIT_E4typeES4_[_ZN3fmt2v68internal11to_unsignedIlEENSt13make_unsignedIT_E4typeES4_]+0x2b): undefined reference to `fmt::v6::internal::assert_fail(char const*, int, char const*)'
collect2: error: ld returned 1 exit status

Do you have any ideas what went wrong? I don't usually link non-standard libraries so I don't have any idea what to do about it.

bartx 3
  • 41
  • 5
  • [And this one](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) –  Feb 18 '22 at 19:37
  • 2
    @OrkhanAliyev Minor point: [this post](https://stackoverflow.com/questions/12573816) is what is referred to as an RTFM target. It's frowned upon to use it as a target just because a question happens to have some form of undefined references. A specific target that covers the specific cause of the undefined reference can be used as a target though, such as the other one you linked (thanks for that btw, I've used it as a target on this question). – cigien Feb 18 '22 at 19:41
  • Ok. Sorry for that. I didn't think of checking just "undefined reference" without specific context of fmt. – bartx 3 Feb 18 '22 at 19:48
  • Side note. Having a space in source file names, while legal, is really asking for trouble with build tools. – prapin Feb 18 '22 at 20:25
  • @bartx3 Nothing to be sorry about. First, marking a question as duplicate is not a sanction (good duplicates are already hard to find when you know they exist). Then, I don't think it is a good duplicate as the solution for your problem is there but only implicitly (you were trying to link the library, you weren't aware of the importance of the ordering). And in the RTFM target, you'd have to go to the fifth answer to get an hint to your problem. – AProgrammer Feb 18 '22 at 20:31

1 Answers1

1

Use

$ g++ -std=c++17 dynamic\ mem.cc -lfmt

fmt is provided as a static library (.a). With those, the order is important as the linker takes out of a library only the objects which are needed to provide symbols to other objects or libraries which precede them in the command line. If you start with a library, there is only main which is missing and usually libraries don't provide main, so they are ignored. When putting the library after your source code, the symbols missing in your code are searched in the library.

(In case of circular dependencies, you may even have to provide a library several times)

AProgrammer
  • 51,233
  • 8
  • 91
  • 143