[opensuse-programming] yet another -static build problem

Hello, does anybody have a problem with static building/compilation? I try to build some simple code with static libraries, but it doesn’t work for me. $ gcc snmp.c -o snmp -static `net-snmp-config --cflags` `net-snmp-config --libs` /usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie collect2: error: ld returned 1 exit status It looks like we have the same problem, like defined here: https://bugzilla.redhat.com/show_bug.cgi?id=785399 https://bugzilla.redhat.com/show_bug.cgi?id=642999 This is my code: ============ #include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h> #include <string.h> int main(int argc, char ** argv) { struct snmp_session session; struct snmp_session *sess_handle; struct snmp_pdu *pdu; struct snmp_pdu *response; struct variable_list *vars; oid id_oid[MAX_OID_LEN]; size_t id_len = MAX_OID_LEN; if(argv[1] == NULL){ printf("Please supply a hostname\n"); exit(1); } init_snmp("APC Check"); snmp_sess_init( &session ); session.version = SNMP_VERSION_2c; session.community = "public"; session.community_len = strlen(session.community); session.peername = argv[1]; sess_handle = snmp_open(&session); pdu = snmp_pdu_create(SNMP_MSG_GET); read_objid("SNMPv2-MIB::sysDescr.0", id_oid, &id_len); snmp_add_null_var(pdu, id_oid, id_len); if ((snmp_synch_response(sess_handle, pdu, &response)) == 0) for(vars = response->variables; vars; vars = vars->next_variable) print_value(vars->name, vars->name_length, vars); else printf("NO SNMP\n"); snmp_free_pdu(response); snmp_close(sess_handle); return (0); } ============= Alex -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse-programming+owner@opensuse.org

For one thing you need to compile with -fPIE. Since -static is a linker opiton, you need to tell the compiler to generate relocatable code. You also need to add the linker option -pie. Modern compilers like to generate builtin functions, and -PIE will generate either a call to the libc.a version along with the appropriate linker relocations. Also, in some cases argv[1] could contain garbage. A more reliable form is to test argc: For example: if(argc < 2){ printf("Please supply a hostname\n"); exit(1); } On 02/04/2015 11:28 AM, Alex Naumov wrote:
Hello,
does anybody have a problem with static building/compilation?
I try to build some simple code with static libraries, but it doesn’t work for me.
$ gcc snmp.c -o snmp -static `net-snmp-config --cflags` `net-snmp-config --libs` /usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie collect2: error: ld returned 1 exit status
It looks like we have the same problem, like defined here: https://bugzilla.redhat.com/show_bug.cgi?id=785399 https://bugzilla.redhat.com/show_bug.cgi?id=642999
This is my code: ============ #include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h> #include <string.h>
int main(int argc, char ** argv) { struct snmp_session session; struct snmp_session *sess_handle; struct snmp_pdu *pdu; struct snmp_pdu *response; struct variable_list *vars;
oid id_oid[MAX_OID_LEN];
size_t id_len = MAX_OID_LEN;
if(argv[1] == NULL){ printf("Please supply a hostname\n"); exit(1); }
init_snmp("APC Check");
snmp_sess_init( &session ); session.version = SNMP_VERSION_2c; session.community = "public"; session.community_len = strlen(session.community); session.peername = argv[1]; sess_handle = snmp_open(&session);
pdu = snmp_pdu_create(SNMP_MSG_GET);
read_objid("SNMPv2-MIB::sysDescr.0", id_oid, &id_len); snmp_add_null_var(pdu, id_oid, id_len);
if ((snmp_synch_response(sess_handle, pdu, &response)) == 0) for(vars = response->variables; vars; vars = vars->next_variable) print_value(vars->name, vars->name_length, vars); else printf("NO SNMP\n");
snmp_free_pdu(response); snmp_close(sess_handle);
return (0); } =============
Alex
-- Jerry Feldman <gaf@blu.org> Boston Linux and Unix PGP key id:B7F14F2F PGP Key fingerprint: D937 A424 4836 E052 2E1B 8DC6 24D7 000F B7F1 4F2F
participants (2)
-
Alex Naumov
-
Jerry Feldman