Cannot compile LibOTR - c++

I am trying to use libotr but I have the following problem when attempting to compile a very basic library initialisation.
#include <libotr/proto.h>
int main(int argc, char const *argv[])
{
OTRL_INIT;
// OtrlUserState userstate = otrl_userstate_create();
return 0;
}
I am compiling it with the following command:
g++ main.cpp -o main -L /usr/local/lib/ -lotr
But for some reason I am getting:
Undefined symbols for architecture x86_64:
"otrl_init(unsigned int, unsigned int, unsigned int)", 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)
make: *** [bin/bin] Error 1
I explicitly checked and the library does indeed has the following symbols.

After a quick observation I noticed that libotr is using the C type name mangling and the problem is resolved just by adding the following lines to the library's include clause:
extern "C" {
#include <libotr/proto.h>
}
If you have similar problem just list the symbols of a library with the nm utility and check whether the symbol names begin with one or two underscores: _foo is C style, while __foo is C++ style.
P.S. I posted this since it took me a while to figure it out. I hope this question + answer would save you some time.

Related

Undefined symbols for architecture arm for

I am new to C++, and trying to use get_string, but I am not sure what I writing wrong that is creating an error.
The code I have is the following:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
string name = get_string("What's your name? ");
printf("hello, %s\n", name);
}
and it keeps saying the following error:
Undefined symbols for architecture arm64:
"_get_string", referenced from:
_main in hello-890d43.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [hello] Error 1
Does any one know what I am doing wrong?
I expected the code take an input and print out hello, (your input).
The same thing happened to me when trying to use the cs50 C library on my local Mac with M1 chip.
Even after installing the library as described here: https://cs50.readthedocs.io/libraries/cs50/c/
It still didn't work.
The problem is coming from the fact that the make command here doesn't include the link command by default.
Assuming your source file is named hello.c instead of just doing:
make hello
You have to enter the following in your terminal in order to compile:
clang hello.c -o hello -lcs50
Afterwards, when running the executable, it works as expected:
./hello
make hello LDLIBS="-lcs50"
LDLIBS to include the cs50 library

Using armadillo in atom with script

I there a way to compile a c++ file using script in atom that uses armadillo? I couldn't find anything related to it.
I already installed armadillo and tried compiling some basic code using script in atom:
#include <iostream>
#include <armadillo>
int main(int argc, const char **argv) {
arma::arma_rng::set_seed_random();
arma::Mat<double> A = arma::randu(4,4);
std::cout << "A:\n" << A << "\n";
return 0;
}
This is the error I got:
Undefined symbols for architecture x86_64:
"thread-local wrapper routine for arma::arma_rng_cxx11_instance", referenced from:
arma::arma_rng::set_seed(unsigned long long) in test1-83e853.o
arma::arma_rng::randu<double>::fill(double*, unsigned long long) in test1-83e853.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 read the source code for this atom-script thing correctly, it just hardcodes -Wall -include stdio.h as compilation options. You need to add -larmadillo to link it to the Armadillo library.
In short: you cannot use atom-script in combination with custom libraries, unless you edit the extension to hardcode the libraries in your compilation commandline.

How to use the fmt library without getting "Undefined symbols for architecture x86_64"

I'm trying to use the fmt (https://github.com/fmtlib/fmt) formatting header library in my c++ project.
I've added the path to the core header file at the top of my main file like so:
#include "../third_party/fmt/core.h"
but when I try to call any function like:
string message = fmt::format("The answer is {}", 42);
I get the following error:
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > fmt::v5::internal::vformat<char>(fmt::v5::basic_string_view<char>, fmt::v5::basic_format_args<fmt::v5::buffer_context<char>::type>)", referenced from:
std::__1::basic_string<std::__1::enable_if<internal::is_string<char [17]>::value, fmt::v5::internal::char_t<char [17]>::type>::type, std::__1::char_traits<std::__1::enable_if<internal::is_string<char [17]>::value, fmt::v5::internal::char_t<char [17]>::type>::type>, std::__1::allocator<std::__1::enable_if<internal::is_string<char [17]>::value, fmt::v5::internal::char_t<char [17]>::type>::type> > fmt::v5::format<char [17], int>(char const (&) [17], int const&) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [main] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
I'm not sure how to use this as this is how I have used other header libraries in the past such as cxxopts. Any help would be appreciated!
You should link with the fmt library or use the optional header-only mode.
For example, if you have the file test.cc:
#include <fmt/core.h>
int main() {
fmt::print("The answer is {}.", 42);
}
You can compile and link it with gcc:
g++ -std=c++11 test.cc -lfmt
From a comment in #vitaut's answer, if you change your #include line from this:
#include "../third_party/fmt/core.h"
to this:
#include "../third_party/fmt/format.h"
it will cause the code to be compiled in "header-only mode", and you won't need to change your build process to compile and link in the {fmt} library.
I'm working on Mac, and I did not realize that you can install the library using brew. It appears at the end of the page. I have been dealing with symbol errors all evening, and I'm not sure that all my problems were related to the build process. The compiling process was also not working properly.
The paths where the library is installed are: /usr/local/include and /usr/local/lib.
I'm using g++-11 to build my project and this instruction works for me:
g++-11 -std=c++20 -I/usr/local/include -L/usr/local/lib -lfmt main.cpp -o main
The only problem is that it works partially. It works fine with print:
fmt::print("Don't {}!\n", "panic");
But it breaks using format:
fmt::format("Don't {}!\n", "panic");
I'm missing something, but I'm not sure what.
By the way, if you are using VSCode, you can create a c_cpp_properties.json into your .vscode folder and add the include path for the headers.
{
"includePath": [
[...],
"/usr/local/include/"
],
}
Not sure if this is related to your case, but I hope it helps.

libuvc program does not compile

I'm trying to use libuvc in one of my C/C++ projects. I succesfully compiled and installed both libusb and libuvc from source and when I attempt to compile the following code using gcc:
#include "libuvc/libuvc.h"
int main (int argc, const char **argv) {
uvc_init(NULL, NULL);
}
I get the following error:
Undefined symbols for architecture x86_64:
"_uvc_init", referenced from:
_main in main-de2855.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'm on MacOS High Sierra 10.13.1 x64.
I used the command:
gcc main.c -luvc -o main
and it worked! Adding the -luvc flag solved my problem, but I don't understand why I need to use it. I've never needed flags before when working with other C/C++ dependencies.

C++ will not run on my mac, symbol(s) not found for architecture x86_64

Heres the code, extremely basic Cpp
#include <iostream>
using namespace std;
int main(){
cout << "C++ is FUN!\n";
return 0;
}
The symbols that can not be found are "std" trying to use the name space, and "cout".
the full error message is.
make: *** [FirstProject] Error 1 FirstProject C/C++ Problem
Symbol 'cout' could not be resolved FirstProgram.cpp /FirstProject line 5 Semantic Error
Symbol 'std' could not be resolved FirstProgram.cpp /FirstProject line 2 Semantic Error
symbol(s) not found for architecture x86_64 FirstProject C/C++ Problem
EDIT:
here is the whole linker line:
make all
Building target: FirstProject
Invoking: Cross G++ Linker
g++ -o "FirstProject" ./FirstProgram.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [FirstProject] Error 1
Does anyone know what could potentially be the problem?
You are not compiling using a C++ compiler.
If you are using the GNU toolchain then use g++ and not gcc.
You need to compile then link:
g++ -c -o FirstProgram.o FirstProgram.c
g++ -o FirstProject FirstProgram.o
Or you can combine into one statement:
g++ -o FirstProject FirstProgram.c