English 中文(简体)
连接共享图书馆
原标题:Linking a shared library

将共用图书馆连接起来,在<代码>main中留下一个未明确的提法。 确切错误是:

/usr/bin/ld: /tmp/cc3pGAwi.o: in function `main :
main.c:(.text+0x1e): undefined reference to `sum 
collect2: error: ld returned 1 exit status

我的档案如下:

build.sh

#!/usr/bin/sh

PROJECT_NAME="foo"
CFLAGS="-Wall -Wextra -pedantic"

gcc $CFLAGS -shared -fPIC -o libsubmodule.so submodule.c
gcc $CFLAGS -o $PROJECT_NAME -L. -Wl,-rpath=. -lsubmodule main.c

main.c

#include <stdio.h>
#include "submodule.h"
int main(void)
{
  printf("%s", "Help Me!!!
");
  printf("%i
",sum(2, 3));
  return 0;
}

submodule.h

#ifndef SUBMODULE_H_
#define SUBMODULE_H_
int sum(int a, int b);
#endif // SUBMODULE_H_

submodule.c

#include <stdio.h>
#include "submodule.h"
int sum(int a, int b)
{
  return a + b;
}

我期望这一方案能够汇编和印刷。

Help me!!!
5

页: 1

If I directly compile submodule.c into my executable, the program behaves as expected.

我认为我的分部分。 由于<代码>nm libsubmodule,所以工作罚款。 因此,有线

00000000000010f9 T sum

我认为,我的守则应当发挥作用,因为我是#include,其中使用-L.-Wl,-rpath=.,以向联系人说明如何找到图书馆,我正在通过-lsubmodule将其与我的可执行链接。

我确信,Im认为图书馆是正确的,因为它会带来不同的错误。 我也认为我觉得我是正确的。 我不理解图书馆如何能包含<代码>sum,可成功与可执行部分联系起来,然后没有发现其职能。 EDIT:我认为,该图书馆是联系人发现的,但可以与方案挂钩。

问题回答

try this:

#!/usr/bin/sh

PROJECT_NAME="foo"
CFLAGS="-Wall -Wextra -pedantic"

gcc $CFLAGS -shared -fPIC -o libsubmodule.so submodule.c main.c
gcc $CFLAGS -o $PROJECT_NAME -L. -Wl,-rpath=. -lsubmodule main.c

低于指挥:

bash build.sh
./foo

页: 1

Help Me!!!
5

情况如下:

“在座的影像描述”/</a

我们来到这里。 首先,通过“Tom”向我表明,我远未理解。 在这方面,我正在寻求的解决办法。 基本上,我的问题是,我把平衡分层联系起来。 因此,我无法执行,但我并没有把图书馆装上记忆或阅读内容。 “未明确提及数额”是因为我需要人工开放和通过图书馆进行搜索,并在记忆中找到一个点。 解决办法围绕的是 d和 d,两者都有非常好的手页。 以下是新的建筑。 sh and 主 席:c.

#!/usr/bin/sh

PROJECT_NAME="funky_pointers"
CFLAGS="-Wall -Wextra"
LIBS="-ldl" // Gotta link the libdl system library

gcc $CFLAGS -shared -fPIC -o libsubmodule.so submodule.c // don t need to put 主 席:c in the .so like @Tom suggested
gcc $CFLAGS -o $PROJECT_NAME -L. -Wl,-rpath=. -lsubmodule $LIBS 主 席:c

主 席:c

#include <stdio.h>
#include <dlfcn.h>
#include "submodule.h"

int main(void)
{
  printf("%s", "Watch this!
");

  const char *submodule_name = "libsubmodule.so";
  void *submodule = dlopen(submodule_name, RTLD_NOW); // get a handle to the .so
  if (submodule == NULL)
  {
    fprintf(stderr, "Failed to load %s
", submodule_name);
    return 1;
  }
  printf("Loading success!
");
  int (*sum)(int, int) = dlsym(submodule, "sum"); // get a pointer to the sum function

  int out = sum(2, 3); // profit
  printf("%i
", out);
  return 0;
}

我希望这对我的0x开发商很有帮助。 !!





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签