How to use fmt library in the header-only mode? - c++

Having a hard time using the header-only mode of fmt library. Here is what I tried in details:
I downloaded fmt7.1.3 from https://fmt.dev/latest/index.html, only put the directory fmt-7.1.3/include/fmt in a directory ([trgdir]) and wrote a test.cpp as follow:
#include <iostream>
#include <fmt/format.h>
int main() {
fmt::format("The answer is {}.", 42);
return 0;
}
Then in the terminal I use
gcc -I[trgdir] test.cpp
where gcc I defined as
alias gcc='gcc-10 -xc++ -lstdc++ -shared-libgcc -std=c++17 -O2 '
I got the error goes as
Undefined symbols for architecture x86_64:
"__ZN3fmt2v76detail7vformatB5cxx11ENS0_17basic_string_viewIcEENS0_11format_argsE", referenced from:
_main in ccEeTo0w.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I have checked this post but I still cannot solve my issue. How to use the fmt library without getting "Undefined symbols for architecture x86_64"

You need to define a macro before include, like this:
#define FMT_HEADER_ONLY
#include "fmt/format.h"

Related

Linking C++ code to a DYLIB library in macOS

I was able to setup BlockSci on macOS v10.13 (High Sierra) 10.13.6. The setup installed header files in /usr/local/include and a libblocksci.dylib in /usr/local/lib. The C++ code I am trying to compile is:
#include "blocksci.hpp"
#include <iostream>
#include <string>
int main(int argc, const char * argv[]) {
blocksci::Blockchain chain{"path/config.json"};
return 0;
};
The compile command I am using for hello.cpp is:
g++ -std=c++17 -L/usr/local/lib -I/usr/local/include/blocksci -I/usr/local/include/blocksci/external -o hello hello.cpp
However, the symbols for the BlockSci library are not found:
Undefined symbols for architecture x86_64:
"blocksci::Blockchain::Blockchain(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
_main in hello-942a60.o
"blocksci::Blockchain::~Blockchain()", referenced from:
_main in hello-942a60.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What am I doing wrong when I try to compile this?
I found this and this that were helpful. It finally compiled with:
g++ hello.cpp -std=c++17 -I/usr/local/include/blocksci/external -o hello -L/usr/local/lib -lblocksci -Wl,-rpath,/usr/local/lib
However, now I get a runtime error:
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error
Abort trap: 6
Back to the drawing board, but it compiled for now.

“Undefined symbol _main” when linking OpenMP on macOS Catalina

I just installed OpenMP by running
$ brew install gcc
Than I ran my code using
$ gcc-10 -o task -fopenmp task.c
However I got the error message:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
My code is:
#include <omp.h>
Can someone help me?
I solved this problem replacing gcc with g++

OpenCV Undefined symbols for architecture x86_64 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I've installed OpenCV 4.0.0 with this command
brew install hybridgroup/tools/opencv
And I'm trying to compile simple program which loads image from filesystem into cv::Mat.
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
int main() {
auto img = cv::imread("/Users/peter/Dektop/source.png");
return 0;
}
And I'm keep getting this error
Undefined symbols for architecture x86_64:
"cv::imread(std::__cxx11::basic_string,
std::allocator > const&, int)", referenced from:
_main in ccw6DQpj.o ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status
I'm compiling with this command. GCC 8.2.0.
g++-8 main.cpp -std=c++17 `pkg-config --cflags --libs opencv4`
Given the answers in the comments, you have a binary incompatible version of the STL.
OpenCV4 is build with libc++, which defines the symbol of string as std::v1::basic_string, while libstdc++ (default of GCC), uses std::__cxx11::basic_string
At compile time, you don't know the difference between these 2 usages, as you are using the same interface. However, the implementation is different.
As you are calling, a function that requires such string, you'll need to compile with the same version of the STL.
I'm sure that for Clang, you can pass -stdlib=libc++ to indicate this version of the STL, I haven't tried it with GCC.

Firebase : Undefined symbols for architecture x86_64 (Configure Multiple project)

I am trying to compile main.ccp to configure my c++ project manually and have it connect to my own firebase.
#include "main.hpp"
#include <iostream>
#include "firebase/app.h"
#include "firebase/database.h"
int main()
{
firebase::AppOptions secondary_app_options;
secondary_app_options.set_api_key("API_KEY");
secondary_app_options.set_app_id("APP_ID");
secondary_app_options.set_project_id("PROJ_ID");
firebase::App* secondary_app = firebase::App::Create(secondary_app_options, "Secondary");
firebase::database::Database* secondary_database = firebase::database::Database::GetInstance(secondary_app);
return 0;
}
I use the command line to compile : g++ -std=c++11 main.cpp -o main.o
And it output:
Undefined symbols for architecture x86_64:
"firebase::App::Create(firebase::AppOptions const&, char const*)", referenced from:
_main in main-9988c0.o
"firebase::database::Database::GetInstance(firebase::App*, firebase::InitResult*)", referenced from:
_main in main-9988c0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I did correctly download and locate all of the framework and library for firebase already. Can anybody help me with this ?

c++: Undefined symbols for architecture x86_64 in Xcode

At first I am trying to use glad.h in my project. Xcode 7.2.1. gives me the error :
glad.h:26:2: OpenGL header already included, remove this include, glad already provides it.
These are the headers in my main.cpp :
#include "glad/glad.h"
#include <iostream>
#include "GLFW/glfw3.h"
I add GLFW_INCLUDE_NONE to packaging - preprocessor definitions and LLVM - preprocessor macros(similar to preprocessor definitions in Visual Studio I guess?). Then the errors turn to:
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main.o
"_glfwGetPrimaryMonitor", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I remove the code #include "glad/glad.h”, the errors remain the same.
Any help is appreciated.