Cannot link dlfcn functions with gcc and -ldl - gcc

I am trying to use functions in dlcfn.h, to print custom trace from my C application.
I do link with -ldl, yet the linker complains that it cannot find dladdr().
What am I missing?
Here is an example, inst.c:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
__attribute__((no_instrument_function))
void __cyg_profile_func_enter (void *this_fn, void *call_site) {
printf("%p\n", this_fn);
Dl_info info;
if (dladdr(this_fn, &info)) {
printf("%p [%s] %s\n",
this_fn,
info.dli_fname ? info.dli_fname : "?",
info.dli_sname ? info.dli_sname : "?");
}
}
__attribute__((no_instrument_function))
void __cyg_profile_func_exit (void *this_fn, void *call_site) {
}
void my_function_b(void)
{
printf("b!\n");
}
void my_function_a(void)
{
printf("a!\n");
my_function_b();
}
int main(void)
{
my_function_a();
my_function_b();
return 0;
}
And a Makefile:
CFLAGS += -O0 -g -finstrument-functions -rdynamic -ldl
inst: inst.c
gcc $(CFLAGS) $^ -o $#
The result:
$ make
gcc -O0 -g -finstrument-functions -ldl inst.c -o inst
/usr/bin/ld: /tmp/ccKL1sh2.o: in function `__cyg_profile_func_enter':
/home/gauthier/tmp/inst/inst.c:12: undefined reference to `dladdr'
collect2: error: ld returned 1 exit status
make: *** [Makefile:4: inst] Error 1

You should put libraries after source or object files which import their functions, otherwise linker will optimize them out:
gcc -O0 -g -finstrument-functions inst.c -ldl -o inst

Related

Undefined reference to sequence when trying to input a vector

This seems a simple but somehow the compile sends this error message which I'm not able understand thus correct my code.
This is a simplified version of what I did, just so it can appear the error for you:
Main.cpp
include "myfunction.h"
int main(){
std::vector<int> myVet = {1,4,3};
sequence(1,2,1,myVet);
}
myfunction.h
#include <vector>
/*funtion creates a sequence*/
void sequence(int start, int end,
int step, std::vector<int> skip);
myfunction.cpp
#include "myfunction.h"
void sequence(int start, int end,
int step, std::vector<int> skip){
auto x = 0;
};
This gives me an error message which says
In function 'main':
/home/machina/Documents/Grafos&Redes/Implementação/main.cpp:18: undefined reference to 'sequence(int, int, int, std::vector <int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status
Could you please explain me why it appears?
This is the following command which I've been using for compiling
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp -o main.out
You are only passing main.cpp to g++.
g++ needs to know about myfunction.cpp where your function is defined so as to compile and link it to your program.
The command to use should be:
g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp myfunction.cpp -o main.out

what is the gcc option for add header file in complie time?

How to compile this code?
/* main.c file*/
#include <stdio.h>
int main(void)
{
int c;
c = add(5,5);
printf("sum is %d\n",c);
return 0;
}
Add.c(in same directory)
/*add.c*/
int add(int a, int b)
{
int c;
c = a + b;
return c;
}
Add.h(in same directory)
/* add.h file*/
int add(int, int);
Then i am create a object file for add.c
$ gcc -c -Wall add.c -I.
Then i am try to create a object file for main.c
$ gcc -c -Wall main.c -I.
main.c:6:2: warning: implicit declaration of function ‘add’ [-Wimplicit-function-declaration]
c = add(5, 5);
^
Please any one tell, how to give the headfile name in compiling time.
you can either use #include "Add.h" at the top of your main.c (this is the preferred route), or you can use the -include command line flag (this is discouraged because it does not scale and is not portable):
gcc -c -Wall main.c -I. -include Add.h

How to use shared library

These are my C codes simply print “Hello" Message. And I want to make mylib.c as shared library.
[mylib.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
int mylib(){
printf("### Hello I am mylib #####\n");
return 0;
}
[drive.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
At the firest I compiled mylib.c with folowing command line to make mylib.o
gcc –fPIC –g –c –Wall mylib.c
Then tried to make it shared librarly like this
gcc -shared -Wl,-soname,libmylib.so.1 -o /opt/lib/libmylib.so.1.0.1 mylib.o -lc
And I did ldconfig to update /etc/ld.so.cache
Finaly I compiled drive.c link with mylib but linker showed error
gcc -g -Wall -Wextra -pedantic -I./ -L./ -o drive drive.c –lmylib
/usr/bin/ld: cannot find –lmylib
Dose someone tell me how can I compile it?
In my way, you have to follow some ways to use shared library in C.
At first I have created a header file named "shared_library.h", in this file I have introduced a function named "method" as a function of this library.
The code is following:
/*-------This is starting of shared_library.h file-----------*/
void method();
/*-------------This is ending of shared_library.h file--------*/
Then I have defined the method in another file named "shared_library.c". The definition as in code is:
/*-------------This is starting of shared_library.c file---------*/
#include "shared_library.h"
void method()
{
printf("Method is called");
}
/*-------------This is ending of shared_library.c file---------*/
And finally, the header "shared_library.h" is ready to use. I use the library in my main C file named "main.c". The contents of "main.c" are as follows:
/*-------------This is starting of main.c file----------------*/
#include <stdio.h>
#include "shared_library.h"
int main()
{
method();
return 0;
}
/*-------------This is ending of main.c file----------------\*/
I found this article ld cannot find an existing library.
It works if I change to gcc -g -Wall -Wextra -pedantic -I./ -L/opt/lib -o drive drive.c –l:libmylib.so.1

Link Object File GCC/G++

I have my Main C++ Class main.cpp...
#include "fs.h"
int main(void)
{
return minit();
}
The fs.h:
#ifndef __FS__
#define __FS__
int minit (void);
#endif
And a fs.o (with minit() into) file that is already an object file, compiled with g++ without -g.
Here is my makefile:
myfs: main.o fs.o
g++ -o myfs -m32 -Wall fs.o main.o
main.o: main.cpp fs.h
g++ -o main.o -m32 main.cpp
Every time I try to link everything, the linker says that in main.cpp there is a undefined reference to minit(); What could it be?
You have defined a function called minit() within actually IMPLEMENTING it - that is why you are having this problem.
You need to actually write the function minit():
int minit(void) {
return 0;
}
For example...
You should point it to compiler that the function is defined elsewhere. Try changing this declaration:
int minit (void);
to this
extern int minit (void);

GCC Creating new PASS

I am trying to write a new GCC pass in gcc-4.1.2. This is my first attempt and I am trying to print lines of code for a function gcc is compiling. So I added the below code. Note if I remove code in execute_gimple_manipulation method the issue does not occur :
gcc/gcc-4.1.2 1223> cat gcc/gimple-manipulation.c
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "ggc.h"
#include "flags.h"
#include "tree.h"
#include "basic-block.h"
#include "tree-flow.h"
#include "tree-pass.h"
#include "tree-dump.h"
#include "timevar.h"
#include "diagnostic.h"
#include "cfgloop.h"
#include "tree-scalar-evolution.h"
#include "tree-ssa-propagate.h"
#include "tree-chrec.h"
static void execute_gimple_manipulation (void);
static bool gate_gimple_manipulation (void);
/*-------------------------
The main driver function.
-------------------------*/
static void execute_gimple_manipulation (void)
{
basic_block bb = ENTRY_BLOCK_PTR;
int bbcounter=0, stmtcounter;
tree stmt;
block_stmt_iterator si;
/* traverse each basic block and print all statements */
FOR_EACH_BB (bb){ /* in any order */
bbcounter++;
printf("\n-> entering bb # %d (internal #: %d)\n", bbcounter, bb->index);
stmtcounter=0;
for(si = bsi_start(bb); !bsi_end_p(si); bsi_next(&si)){
stmtcounter++;
printf(" encountering statement #%2d: ", stmtcounter);
stmt = bsi_stmt(si);
print_generic_stmt (stderr, stmt, 0);
}
}
}
/* ------------------------------------------
Return true if we should execute our pass.
------------------------------------------*/
static bool
gate_gimple_manipulation (void)
{
/* return (flag_unit_at_a_time != 0 && flag_ipa_gimple_manipulation
Don't bother doing anything if the program has errors.
&& !(errorcount || sorrycount));*/
return (/*optimize >= 1*/
/* Don't bother doing anything if the program has errors. */
/*&&*/ !(errorcount || sorrycount));
}
struct tree_opt_pass pass_gimple_manipulation =
{
"gm_pass_simple", /* name */
gate_gimple_manipulation, /* gate */
execute_gimple_manipulation, /* execute */
NULL, /* sub */
NULL, /* next */
0, /* static pass number */
0, /* tv_id */
PROP_cfg | PROP_ssa | PROP_alias, /* properties required */
0, /* properties provided */
0, /* properties destroyed */
0, /* todo_flags start */
TODO_dump_func, /* todo_flags finish */
0 /* letter */
};
My new code compilation goes fine as per make:
gcc -c -g -O2 -DIN_GCC -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -pedantic -Wno-long-long -Wno-variadic-macros -Wold-style-definition -Wmissing-format-attribute -DHAVE_CONFIG_H -I. -I. -I../.././gcc -I../.././gcc/. -I../.././gcc/../include -I../.././gcc/../libcpp/include ../.././gcc/gimple-manipulation.c -o gimple-manipulation.o
But the compilation process fails further on:
gcc/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc/xgcc -Bgcc/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc/ -B/usr/local/x86_64-unknown-linux-gnu/bin/ -B/usr/local/x86_64-unknown-linux-gnu/lib/ -isystem /usr/local/x86_64-unknown-linux-gnu/include -isystem /usr/local/x86_64-unknown-linux-gnu/sys-include -O2 -O2 -g -O2 -DIN_GCC -W -Wall -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -isystem ./include -fPIC -g -DHAVE_GTHR_DEFAULT -DIN_LIBGCC2 -D__GCC_FLOAT_NOT_NEEDED -msse -c \
../.././gcc/config/i386/crtfastmath.c \
-o crtfastmath.o
../.././gcc/config/i386/crtfastmath.c:110: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
make[2]: *** [crtfastmath.o] Error 1
make[2]: Leaving directory `gcc/gcc-4.1.2/host-x86_64-unknown-linux-gnu/gcc'
make[1]: *** [all-gcc] Error 2
make[1]: Leaving directory `gcc/gcc-4.1.2'
make: *** [all] Error 2
gcc/gcc-4.1.2 1223>
Any help – why the issue is occurring and how can I achieve the objective?

Resources