How to use GcrSecureEntryBuffer in a GTK3 application? - user-interface

I want to use a GcrSecureEntryBuffer for a GtkEntry, because the user will enter passwords in it. I found the following links.
Gnome Developer's Manual: https://developer.gnome.org/gcr/stable/GcrSecureEntryBuffer.html
GCR Library Reference Manual: https://www.manpagez.com/html/gcr-3/gcr-3-3.28.0/
The GCR project: https://gitlab.gnome.org/GNOME/gcr
I wrote this this toy program ztest.c:
#include <gck/gck.h>
#include <gcr/gcr.h>
#include <gtk/gtk.h>
int main(void)
{
gtk_init(0, NULL);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *entry = gtk_entry_new_with_buffer(gcr_secure_entry_buffer_new());
gtk_container_add(GTK_CONTAINER(window), entry);
gtk_widget_show_all(window);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
}
and installed the required libraries.
sudo apt install libgtk-3-dev libgcr-3-dev gcr p11-kit p11-kit-modules
Based on what I learnt from the GCR and GCK headers, I compiled this code with
gcc -DGCR_API_SUBJECT_TO_CHANGE -DGCK_API_SUBJECT_TO_CHANGE $(pkg-config --cflags gtk+-3.0) -I/usr/include/gcr-3 -I/usr/include/gck-1/ -I/usr/include/p11-kit-1/ -o test ztest.c $(pkg-config --libs gtk+-3.0)
and got the following error. (There were no other warnings about undefined functions.)
/usr/bin/ld: /tmp/ccxVu7EL.o: in function `main':
ztest.c:(.text+0x26): undefined reference to `gcr_secure_entry_buffer_new'
collect2: error: ld returned 1 exit status
How do I fix this? Is there anything else that must be installed? And is there a pkg-config command for compiler and linker flags for these libraries?

When you talk about "Gcr", you actually talk about 2 different libraries (each with their own pkg-config module)
gcr-3: contains the base encryption stuff
gcr-ui-3: contains code which interacts with GTK (widget and other related code)
GcrSecureEntryBuffer is part of gcr-ui-3, so you should use that for adding the necessary flags.

Related

gcc undefined reference to with pthread [duplicate]

I picked up the following demo off the web from https://computing.llnl.gov/tutorials/pthreads/
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
But when I compile it on my machine (running Ubuntu Linux 9.04) I get the following error:
corey#ubuntu:~/demo$ gcc -o term term.c
term.c: In function ‘main’:
term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
/tmp/cc8BMzwx.o: In function `main':
term.c:(.text+0x82): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
This doesn't make any sense to me, because the header includes pthread.h, which should have the pthread_create function. Any ideas what's going wrong?
For Linux the correct command is:
gcc -pthread -o term term.c
In general, libraries should follow sources and objects on command line, and -lpthread is not an "option", it's a library specification. On a system with only libpthread.a installed,
gcc -lpthread ...
will fail to link.
Read this or this detailed explanation.
For Linux the correct command is:
gcc -o term term.c -lpthread
you have to put -lpthread just after the compile command,this command will tell to the compiler to execute program with pthread.h library.
gcc -l links with a library file.Link -l with library name without the lib prefix.
in eclipse
properties->c/c++Build->setting->GCC C++ linker->libraries in top part add "pthread"
Running from the Linux terminal, what worked for me was compiling using the following command (suppose the c file I want to compile is called test.c):
gcc -o test test.c -pthread
Hope it helps somebody!
If you are using cmake, you can use:
add_compile_options(-pthread)
Or
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
I believe the proper way of adding pthread in CMake is with the following
find_package (Threads REQUIRED)
target_link_libraries(helloworld
${CMAKE_THREAD_LIBS_INIT}
)
Acutally, it gives several examples of compile commands used for pthreads codes are listed in the table below, if you continue reading the following tutorial:
https://computing.llnl.gov/tutorials/pthreads/#Compiling
Compile it like this : gcc demo.c -o demo -pthread
In Visual Studio 2019 specify -pthread in the property pages for the project under:
Linker -> Command Line -> Additional Options
Type in -pthread in the textbox.
You need to use the option -lpthread with gcc.
you need only Add "pthread" in proprieties=>C/C++ build=>GCC C++ Linker=>Libraries=> top part "Libraries(-l)".
thats it
check man page and you will get.
Compile and link with -pthread.
SYNOPSIS
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
Compile and link with -pthread.
....
Since none of the answers exactly covered my need (using MSVS Code), I add here my experience with this IDE and CMAKE build tools too.
Step 1: Make sure in your .cpp, (or .hpp if needed) you have included:
#include <functional>
Step 2 For MSVSCode IDE users:
Add this line to your c_cpp_properties.json file:
"compilerArgs": ["-pthread"],
Step 2 For CMAKE build tools users:
Add this line to your CMakeLists.txt
set(CMAKE_CXX_FLAGS "-pthread")
Note: Adding flag -lpthread (instead of -pthread) results in failed linking.
From man gcc,
-pthread
Define additional macros required for using the POSIX threads library.
You should use this option consistently for both compilation and linking.
This option is supported on GNU/Linux targets,
most other Unix derivatives,
and also on x86 Cygwin and MinGW targets.
It is correct that -pthread is an option and the best way to handle this.
There are statements in some answers that it generates different compiled code. This is misleading.
If you wish to duplicate -pthread, you could use -lpthread -D_REENTRANT=1. So there are two things going on with the -pthread option.
Indeed it links with the pthread library as many answers express. Also, the order of the pthread library is important because it may override some weak symbols. So a correct version using -lpthread may need to have it multiple times on the command line.
The other important part is the _REENTRANT define. Note, that this is in the implementation namespace. Some people may care for portability and other not. However, it is very important that it is defined as the first thing in the compilation unit. This symbol will alter the way that many system headers files are parsed.
You can include #define _REENTRANT 1 at the top of every source file, but it is much easier to have it on the command line. Again, the -pthread is the best way to achieve this. Also, gcc may change the way this is implemented in the future. However, I think it is important for programmers to understand what is going on.
term.c: In function ‘main’: term.c:23: warning: incompatible implicit
declaration of built-in function ‘exit’
You never included <stdlib.h>, where exit() is declared. Also, I think newer versions of gcc have removed the need for _REENTRANT.
Why features.h?
Example on godbolt, without -pthread.
So, it is NOT generating different code. Ie, the backend of the compiler is NOT different. It is only conditional compilation and linking to different libraries. It does not generate 'lock free' code or add appropriate machine barriers because you have used this option.
In Anjuta, go to the Build menu, then Configure Project.
In the Configure Options box, add:
LDFLAGS='-lpthread'
Hope it'll help somebody too...
Sometimes, if you use multiple library, check the library dependency.
(e.g. -lpthread -lSDL... <==> ... -lSDL -lpthread)

Link against shared library with SONAME

How can I force the gcc linker to link against a given version (soname) of a shared library on the system?
I need this to enforce that the version of openssl that is #include'ed matches the version that is linked, on any system, even if multiple versions of openssl are installed. To find the ABI version, my configure script first compiles and runs a program that extracts the SONAME from the headers:
#include <openssl/opensslv.h>
#include <stdio.h>
int main () {
printf(SHLIB_VERSION_NUMBER);
return 0;
}
The SHLIB_VERSION_NUMBER contains the so version string, e.g. 0.9.8 or 1.0.2k or 1.1.0. But how do I tell gcc to link against this version of libssl or libcrypto rather than just any -lssl?
I tried "in situ" linking, so that instead of linking with gcc main.c -lcrypto we use:
gcc main.c libcrypto.so.1.1.0
However it seems the linker libcrypto.so.1.1.0 cannot be found:
gcc: error: libcrypto.so.1.1.0: No such file or directory
I guess the system only searches in the standard locations when using the -l flag. Is there a better way to make my software link against libcrypto.so.1.1.0?
To select the correct version of the openssl shared libraries use:
gcc main.c -l:libssl.so.1.0.0 -l:libcrypto.so.1.0.0
The key to answering this question is "how do I control ld so that is links the correct version of a shared library?".
The correct way to pass a linker flag (a command line parameter to ld) using the gnu compilers (gcc, g++, gdc, etc...) is to use the normal ld parameter prefixed with "-l". For example -lssl or -L/usr/local/lib.
Edit: As per How to specify the library version to use at link time? you can read the manual for ld with:man ld.

Linking difference between clang and gcc

I am running fedora 23 and just compiled and installed casadi 2.4.1 (however, I do not think that this is casadi specific).
I am trying to compile and run a simple test program:
#include <iostream>
#include <casadi/casadi.hpp>
using namespace casadi;
int main () {
const SX m(42);
const SX n(23);
const SX x = m + n ;
std::cout << x << std::endl;
}
It works fine with gcc:
$ g++ test.cpp -std=c++11 -I$HOME/local/include -L$HOME/local/lib -lcasadi && ./a.out
SX(65)
But using clang, I get a linker error:
$ clang++ test.cpp -std=c++11 -I$HOME/local/include -L$HOME/local/lib -lcasadi && ./a.out
/tmp/test-cde41f.o: In function `casadi::GenericMatrix<casadi::Matrix<casadi::SXElement> >::dimString() const':
test.cpp:(.text._ZNK6casadi13GenericMatrixINS_6MatrixINS_9SXElementEEEE9dimStringEv[_ZNK6casadi13GenericMatrixINS_6MatrixINS_9SXElementEEEE9dimStringEv]+0x2b): undefined reference to `casadi::Sparsity::dimString() const'
/tmp/test-cde41f.o: In function `void casadi::Sparsity::set<casadi::SXElement>(casadi::SXElement*, casadi::SXElement const*, casadi::Sparsity const&) const':
test.cpp:(.text._ZNK6casadi8Sparsity3setINS_9SXElementEEEvPT_PKS3_RKS0_[_ZNK6casadi8Sparsity3setINS_9SXElementEEEvPT_PKS3_RKS0_]+0x6f0): undefined reference to `casadi::Sparsity::dimString() const'
test.cpp:(.text._ZNK6casadi8Sparsity3setINS_9SXElementEEEvPT_PKS3_RKS0_[_ZNK6casadi8Sparsity3setINS_9SXElementEEEvPT_PKS3_RKS0_]+0x743): undefined reference to `casadi::Sparsity::dimString() const'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am pretty confident that the symbol is defined in the library. However, I fail to understand what clang is missing here. Is there some special c++11 ABI flag that I need to pass to the linker or something like that?
I would suspect version skew. Unless you do something special, clang uses some gcc stdc++ library, for which it searches. That was the case the last time I used clang for much. They may use their own std c++ library now. But if casadi was compiled with one version of gcc and you used clang which uses the std c++ library for another version of gcc, you can see linker errors like this.
You may learn something if you run clang -V to get version information. Also, gcc -v to get gcc version information, and verify that the version of gcc clang wants is the same as the version with which you are compiling.

How to fix undefined reference to `_imp__pthread_create'

I use MinGW on windows7 32bit.
And I can’t compile my source which uses pthread.
My code is below.
#include <stdio.h>
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
printf("######## start \n");
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
}
Error happens as I compile it.
gcc -I /usr/local/include -L /usr/local/lib/libpthread.dll.a trylpthread.c
C:\Users\xxx\AppData\Local\Temp\cc9OVt5b.o:trylpthread.c:(.text+0x25): undefined reference to `_imp__pthread_create'
collect2.exe: error: ld returned 1 exit status
I use following pthread library.
pthreads-w32-2.8.0-3-mingw32-dev
And here is libpthread.dll.a in /usr/local/lib
Does anyone know how to fix this problem?
The commandline:
gcc -I /usr/local/include -L /usr/local/lib/libpthread.dll.a trylpthread.c
does not make sense.
-L <dir> is a linker option that directs the linker to search for required libraries
in directory <dir>. Thus you are telling the linker to search for required libraries in
path /usr/local/lib/libpthread.dll.a, which is not a directory, while on the other hand
you are not telling the linker to link any libraries at all. That is why it fails to find any
definition for _imp__pthread_create.
Neither does the program you have posted make sense. The lines:
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
say:-
If I have not defined a preprocessor macro pthread_create then compile:
return ((int*)(&pthread_create))[argc];
else compile:
(void)argc;
return 0;
Well if you had defined a preprocessor macro pthread_create, e.g.
#define pthread_create whatever
then the code you would compile would be:
(void)argc;
return 0;
And since you have indeed not defined any such macro, the code you compile is:
return ((int*)(&pthread_create))[argc];
which fails at linkage, as you see. And if that code was compiled with pthread_create so defined,
it would be:
return ((int*)(&whatever))[argc];
Rewrite your program as:
#include <stdio.h>
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
printf("######## start \n");
return ((int*)(&pthread_create))[argc];
}
Compile with:
gcc -Wall -I /usr/local/include -o trylpthread.o -c trylpthread.c
Link with:
gcc -o trylpthread.exe trylpthread.o /usr/local/lib/libpthread.dll.a
Remember that when you get the program compiled and linked, the appropriate pthreadGC??.dll
must be found at runtime in one of the places where the program loader searches for dlls.
Better still, uninstall your MinGW and your pthreads-w32-2.8.0-3-mingw32-dev and
install a more up-to-date Windows port of GCC, e.g. TDM-GCC (simplest) or mingw-w64. Pick the 32-bit version, if your Windows system
is 32-bit. These toolchains come with built in pthread support, as GCC standardly does.
Compile with:
gcc -Wall -o trylpthread.o -c trylpthread.c
Link with:
gcc -o trylpthread.exe trylpthread.o -pthread
(not -lpthread)

How do you compile C++ programs that include LLVM API headers?

I'm trying to use the C++ compiler to compile the following program:
#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IR/Module.h"
int main( int argc, char* argv[] )
{
if( argc < 2 )
llvm::errs() << "Expected an argument - IR file name\n";
llvm::LLVMContext &context = llvm::getGlobalContext();
llvm::SMDiagnostic err;
llvm::Module* module = llvm::ParseIRFile( argv[1], err, context );
if( !mod )
{
err.print( argv[0], errs() );
return 1;
}
return 0;
}
I'm trying to compile the program using the following command:
clang++ main.cpp -o main
However, when I compile, I'm getting the following compile error:
main.cpp:2:10: fatal error: 'llvm/IR/LLVMContext.h' file not found
#include "llvm/IR/LLVMContext.h"
^
1 error generated.
In this case, I'm not exactly sure how to link the LLVM API headers when compiling main.cpp with Clang.
Any help would be greatly appreciated.
You can use the following command:
g++ -std=c++11 main.cpp `llvm-config --system-libs --cppflags --ldflags --libs core` -o main
Where --libs and --system-libs flags are used for linking and --cppflags takes care of include paths.
Thank You
You need LLVM checked out or installed somewhere on your system. You can download a binary release (with headers and libraries you can build against) as explained here: http://llvm.org/releases/download.html#3.5
You can also check out LLVM from its SVN repository as explained here: http://llvm.org/docs/GettingStarted.html#checkout
Once you do that, I recommend looking at the llvm-clang-samples repository that comes with a Makefiles showing how to build sample programs vs. an up-to-date LLVM.

Resources