tech1978
tech1978

Reputation: 113

unusual error while linking Howard Hinnat's date time library

I am trying to link following code

#include "date/tz.h"
#include <chrono>
#include <iostream>

int
main()
{
    using namespace date;
    //using namespace std;
    //using namespace chrono;

    auto tz = locate_zone("America/New_York");
    return 0;
}

I am using following compilation lines

g++ -fsigned-char -g -O0 -std=c++17 -I/home/user1/date-master_debug/include -I/home/user1/date-master_debug -m64 -Wfatal-errors -c hh_is_dst.cc -o hh_is_dst.o

g++ -g -O0 -std=c++17 -I/home/user1/date-master_debug/include -m64 -Wfatal-errors -o hh_is_dst hh_is_dst.o -Wl,-Bdynamic -ldl -Wl,-Bstatic -L/home/user1/date-master_debug/build -ldate-tz -Wl,-Bdynamic -lstdc++ -lgcc -lgcc_eh -Wl,-Bdynamic -lm -lcrypt -lc -lrt -lpthread

Getting following error

hh_is_dst.o: In function `main': /home/user1/hh_is_dst.cc:12: undefined reference to date::locate_zone(std::basic_string_view<char, std::char_traits<char> >)'
collect2: error: ld returned 1 exit status

Any idead why it is asking for above definition ? Or Am I missing something?

I have just compiled date time library with following commands

CXX=g++ CC=gcc CXXFLAGS='-g -O0 -fexceptions -std=c++17' /home/user1/utilities/cmake-3.21.1-linux-x86_64/bin/cmake --debug-trycompile --debug-output -DENABLE_DATE_TESTING=ON -DBUILD_TZ_LIB=ON -DMANUAL_TZ_DB=ON -DUSE_TZ_DB_IN_DOT=ON ../

CXX=g++ CC=gcc CXXFLAGS='-g -O0 -fexceptions -std=c++17' /home/user1/utilities/cmake-3.21.1-linux-x86_64/bin/cmake --build . --target testit --verbose

Library got created with one test case failed.

using following environment

cat /etc/redhat-release

Red Hat Enterprise Linux release 8.10 (Ootpa)

gcc --version

gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-22)

g++ --version

g++ (GCC) 8.5.0 20210514 (Red Hat 8.5.0-22)

Actually I got error when I tried to add code in my application. This is minimal example that I can reproduce.

Upvotes: 0

Views: 83

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 219335

For whatever reason you are not linking to tz.cpp (where date::locate_zone is defined). Imho, the easiest thing to do is not not use the CMake build system and just compile and link to tz.cpp.

I recommend following the build instructions here: https://howardhinnant.github.io/date/tz.html#Installation

Whatever configuration flags you use, you'll need to use the same ones for both your files (that include tz.h) and tz.cpp.

Note that if you can use gcc-14 and C++20, this library is obsoleted by <chrono>. E.g. use std::chrono::locate_zone instead.

Upvotes: 3

Related Questions