为什么 C 头文件中的函数在 main.c 中可用,但在编译时出现未定义的错误?
Why is a function in a C header file available in main.c, but errors as undefined at compile time?
I have a C project in Eclipse (Neon) on Ubuntu 15.10. I have downloaded the OpenSSL library.
I have included openssl/bn.h
in my preprocessor directives. In my main.c
file, I can refer to BN_new()
, a function in bn.h
:
However, when I try to compile, I get an undefined reference for all calls into bn.h
(BN_new()
, BN_free()
, etc).
As a side note, I copied the contents of openssl-1.0.2h/include/openssl
to my /usr/include/
directory.
Why is this error happening at compile time?
my code:
#include <stdio.h>
#include "jpake.h"
#include <openssl/crypto.h>
#include <openssl/sha.h>
#include <openssl/err.h>
#include <openssl/bn.h>
#include <memory.h>
/*
* In the definition, (xa, xb, xc, xd) are Alice's (x1, x2, x3, x4) or
* Bob's (x3, x4, x1, x2). If you see what I mean.
*/
typedef struct
{
char *name; /* Must be unique */
char *peer_name;
BIGNUM *p;
BIGNUM *g;
BIGNUM *q;
BIGNUM *gxc; /* Alice's g^{x3} or Bob's g^{x1} */
BIGNUM *gxd; /* Alice's g^{x4} or Bob's g^{x2} */
} JPAKE_CTX_PUBLIC;
struct JPAKE_CTX
{
JPAKE_CTX_PUBLIC p;
BIGNUM *secret; /* The shared secret */
BN_CTX *ctx;
BIGNUM *xa; /* Alice's x1 or Bob's x3 */
BIGNUM *xb; /* Alice's x2 or Bob's x4 */
BIGNUM *key; /* The calculated (shared) key */
};
static void JPAKE_ZKP_init(JPAKE_ZKP *zkp)
{
zkp->gr = BN_new();
zkp->b = BN_new();
}
static void JPAKE_ZKP_release(JPAKE_ZKP *zkp)
{
BN_free(zkp->b);
BN_free(zkp->gr);
}
typedef struct
{
const char *name; //must be unique
int base; //1 for Alice, 3 for Bob. Only used for printing stuff
}JPakeUserPublic;
typedef struct
{
JPakeUserPublic p;
BIGNUM *secret; //the shared secret
BIGNUM *key; //the calculated (shared) key
BIGNUM *xa; //ALice's x1 or Bob's x3
BIGNUM *xb; //ALice's x2 or Bob's x4
}JPakeUser;
int main(int argc, char **argv)
{
JPakeUser alice, bob;
alice.p.name = "Alice";
alice.p.base = 1;
bob.p.name = "Bob";
bob.p.base = 3;
puts(alice.p.name);
}
I am not a new developer but I AM new to C and eclipse so I have been battling this for awhile. I have been experimenting with the makefile for this project also. Here is my makefile (it is modified from a C++ example):
all: hash4.exe
clean:
rm jpakedemo.o hash4.exe
hash4.exe: jpakedemo.c
gcc -g -o hash4 jpakedemo.c
jpakedemo.o:
gcc -c -g jpakedemo.c
.h
header 文件的目的是描述特定函数的内容 "looks like," 以便生成正确的代码来调用那个函数。
但这本身并不提供功能本身!
"linking all of the various compiler-outputs and libraries into 'a complete thing that can actually be run'" 的最后一个过程完全由一个单独的程序执行......这足够恰当地称为 "linker."
您引用的子例程可能来自多个地方:来自另一个单独编译的源程序,或来自库。 (如果来自图书馆,该图书馆可能是 "statically" 或 "dynamically" 链接。)然而,不知何故, 必须找到必要的目标代码,以便他们可以合并到最终的可执行文件中。
OpenSSL 子例程无疑来自 库 (它是 OpenSSL 的一部分)。因此,您的 Eclipse 项目目前可能不包含对该库的引用,以便在需要时可以找到它。
I have a C project in Eclipse (Neon) on Ubuntu 15.10. I have downloaded the OpenSSL library.
I have included openssl/bn.h
in my preprocessor directives. In my main.c
file, I can refer to BN_new()
, a function in bn.h
:
However, when I try to compile, I get an undefined reference for all calls into bn.h
(BN_new()
, BN_free()
, etc).
As a side note, I copied the contents of openssl-1.0.2h/include/openssl
to my /usr/include/
directory.
Why is this error happening at compile time?
my code:
#include <stdio.h>
#include "jpake.h"
#include <openssl/crypto.h>
#include <openssl/sha.h>
#include <openssl/err.h>
#include <openssl/bn.h>
#include <memory.h>
/*
* In the definition, (xa, xb, xc, xd) are Alice's (x1, x2, x3, x4) or
* Bob's (x3, x4, x1, x2). If you see what I mean.
*/
typedef struct
{
char *name; /* Must be unique */
char *peer_name;
BIGNUM *p;
BIGNUM *g;
BIGNUM *q;
BIGNUM *gxc; /* Alice's g^{x3} or Bob's g^{x1} */
BIGNUM *gxd; /* Alice's g^{x4} or Bob's g^{x2} */
} JPAKE_CTX_PUBLIC;
struct JPAKE_CTX
{
JPAKE_CTX_PUBLIC p;
BIGNUM *secret; /* The shared secret */
BN_CTX *ctx;
BIGNUM *xa; /* Alice's x1 or Bob's x3 */
BIGNUM *xb; /* Alice's x2 or Bob's x4 */
BIGNUM *key; /* The calculated (shared) key */
};
static void JPAKE_ZKP_init(JPAKE_ZKP *zkp)
{
zkp->gr = BN_new();
zkp->b = BN_new();
}
static void JPAKE_ZKP_release(JPAKE_ZKP *zkp)
{
BN_free(zkp->b);
BN_free(zkp->gr);
}
typedef struct
{
const char *name; //must be unique
int base; //1 for Alice, 3 for Bob. Only used for printing stuff
}JPakeUserPublic;
typedef struct
{
JPakeUserPublic p;
BIGNUM *secret; //the shared secret
BIGNUM *key; //the calculated (shared) key
BIGNUM *xa; //ALice's x1 or Bob's x3
BIGNUM *xb; //ALice's x2 or Bob's x4
}JPakeUser;
int main(int argc, char **argv)
{
JPakeUser alice, bob;
alice.p.name = "Alice";
alice.p.base = 1;
bob.p.name = "Bob";
bob.p.base = 3;
puts(alice.p.name);
}
I am not a new developer but I AM new to C and eclipse so I have been battling this for awhile. I have been experimenting with the makefile for this project also. Here is my makefile (it is modified from a C++ example):
all: hash4.exe
clean:
rm jpakedemo.o hash4.exe
hash4.exe: jpakedemo.c
gcc -g -o hash4 jpakedemo.c
jpakedemo.o:
gcc -c -g jpakedemo.c
.h
header 文件的目的是描述特定函数的内容 "looks like," 以便生成正确的代码来调用那个函数。
但这本身并不提供功能本身!
"linking all of the various compiler-outputs and libraries into 'a complete thing that can actually be run'" 的最后一个过程完全由一个单独的程序执行......这足够恰当地称为 "linker."
您引用的子例程可能来自多个地方:来自另一个单独编译的源程序,或来自库。 (如果来自图书馆,该图书馆可能是 "statically" 或 "dynamically" 链接。)然而,不知何故, 必须找到必要的目标代码,以便他们可以合并到最终的可执行文件中。
OpenSSL 子例程无疑来自 库 (它是 OpenSSL 的一部分)。因此,您的 Eclipse 项目目前可能不包含对该库的引用,以便在需要时可以找到它。