C 如何确定当前程序是用作库还是独立可执行文件?

C 如何确定当前程序是用作库还是独立可执行文件?,c,C,我想制作一个输出三个值的简单C程序,我需要该程序输出值,仅当当前程序直接执行且未包含时 这是我的密码: #define Type "type-of-something" #define Info "some-basic-info" #define Todo "something-todo" 要确定当前脚本是否正在Python中导入或直接执行,可以执行以下操作: if __name__ == "__main__": pass 对于如何在C中实现这一点,我一点也不知道,但我的想法是这样的

我想制作一个输出三个值的简单C程序,我需要该程序输出值,仅当当前程序直接执行且未包含时

这是我的密码:

#define Type "type-of-something"
#define Info "some-basic-info"
#define Todo "something-todo"
要确定当前脚本是否正在Python中导入或直接执行,可以执行以下操作:

if __name__ == "__main__":
    pass
对于如何在C中实现这一点,我一点也不知道,但我的想法是这样的:

#include <stdio.h>
void main(void) {
  if(__name__ == "__main__") {
    printf("%d\n", valueX);
  }
  return;
}
#包括
真空总管(真空){
如果(uuuu name_uuuuuu==“uuuuu main_uuuuuuuu”){
printf(“%d\n”,valueX);
}
返回;
}
在C语言中可能有这种情况吗?

如果是这样的话,我能做的最有效的方法是什么?

如果你想做的话,我会选择这样的方法:

#include <stdbool.h>

bool as_library = true;

int main(int argc, char ** argv) {
  as_library = false;
  // ...
  return 0;
}
#包括
bool as_library=true;
int main(int argc,字符**argv){
as_library=false;
// ...
返回0;
}

因为当您的代码被编译并用作程序时,将调用
main
,然后相应地设置该布尔值。

如果您需要,我会选择这样的方法:

#include <stdbool.h>

bool as_library = true;

int main(int argc, char ** argv) {
  as_library = false;
  // ...
  return 0;
}
#包括
bool as_library=true;
int main(int argc,字符**argv){
as_library=false;
// ...
返回0;
}

因为当您的代码被编译并用作程序时,将调用
main
,然后相应地设置布尔值。

这是一个非常人为的问题,我很难看到任何真实世界的应用程序。也就是说,以下内容或多或少适用于带有GCC4.8.4的Ubuntu14.04。您的里程可能会有所不同

这用于将main定义为弱符号

请注意,仍然无法直接执行生成的
mylib。因此
,因为(至少在Linux上)可执行文件和动态链接库有两种不同的ELF格式

我尝试用
-fPIC
编译“共享库”,但没有
-shared
生成可执行的ELF,但是您无法让
ld
将其视为共享库
dlopen
也不起作用

所以我认为这是在*nix+gcc上可以做的最好的了。这将是特定于编译器的,所以谁知道您可以在windows上执行哪些操作

生成文件:

all: test1 test2

#test1 links in mylib.so the "normal" way.
# -Wl,-rpath . is used to search the current directry for mylib.so
# at runtime so the library doesn't have to be "installed".
test1: test.c mylib.so
    gcc -Wl,-rpath . -o $@ $^

#test2 builds an executable out of *just* the library, no other source.    
test2: ./mylib.so
    gcc -Wl,-rpath . -o $@ $^

# Target to compile the .so file.
mylib.so: lib.c
    gcc -shared -o $@ -fPIC $^

# cleanup target.
.PHONY: clean
clean:
    rm -f test1 test2 mylib.so
测试c:

extern void say_hello(const char * msg);

/* pragma weak is the real magic here that allows the library to 
   contain a 'main' -OR- allow application code to define its own
   main. */
#pragma weak main
int main(int argc, char**argv)
{
  say_hello("hello from test main\n");
  return 0;
}
lib.c:

#include <stdio.h>

/* the shared library function used by both programs */
void say_hello(const char * msg)
{
  puts(msg);
}

int main(int argc, char**argv)
{
  say_hello("hi from library's main\n");
}
#包括
/*两个程序使用的共享库函数*/
void say_hello(const char*msg)
{
放(味精);
}
int main(int argc,字符**argv)
{
说你好(“从图书馆的主窗口打招呼”);
}

这是一个非常人为的问题,我很难看到任何真实世界的应用程序。也就是说,以下内容或多或少适用于带有GCC4.8.4的Ubuntu14.04。您的里程可能会有所不同

这用于将main定义为弱符号

请注意,仍然无法直接执行生成的
mylib。因此
,因为(至少在Linux上)可执行文件和动态链接库有两种不同的ELF格式

我尝试用
-fPIC
编译“共享库”,但没有
-shared
生成可执行的ELF,但是您无法让
ld
将其视为共享库
dlopen
也不起作用

所以我认为这是在*nix+gcc上可以做的最好的了。这将是特定于编译器的,所以谁知道您可以在windows上执行哪些操作

生成文件:

all: test1 test2

#test1 links in mylib.so the "normal" way.
# -Wl,-rpath . is used to search the current directry for mylib.so
# at runtime so the library doesn't have to be "installed".
test1: test.c mylib.so
    gcc -Wl,-rpath . -o $@ $^

#test2 builds an executable out of *just* the library, no other source.    
test2: ./mylib.so
    gcc -Wl,-rpath . -o $@ $^

# Target to compile the .so file.
mylib.so: lib.c
    gcc -shared -o $@ -fPIC $^

# cleanup target.
.PHONY: clean
clean:
    rm -f test1 test2 mylib.so
测试c:

extern void say_hello(const char * msg);

/* pragma weak is the real magic here that allows the library to 
   contain a 'main' -OR- allow application code to define its own
   main. */
#pragma weak main
int main(int argc, char**argv)
{
  say_hello("hello from test main\n");
  return 0;
}
lib.c:

#include <stdio.h>

/* the shared library function used by both programs */
void say_hello(const char * msg)
{
  puts(msg);
}

int main(int argc, char**argv)
{
  say_hello("hi from library's main\n");
}
#包括
/*两个程序使用的共享库函数*/
void say_hello(const char*msg)
{
放(味精);
}
int main(int argc,字符**argv)
{
说你好(“从图书馆的主窗口打招呼”);
}

带有
main
的源文件不能在库中。仅在编译时,例如#if def,此#if def必须从obj文件中排除main(){}或准备链接error@FiddlingBits我认为有些库是用
main()编译的
-如果库作为程序执行,则打印使用信息的函数。但是,如果二进制文件用作库,则其
main()
显然不会在启动时被调用。例如:gcc-DCOMPILE\u AS\u LIBRARY=1 source.c-DCOMPILE\u AS\u LIBRARY将从中排除main等compile@Olaf:但这正是我的libc6在Linux上所做的。带有
main
的源文件不能在库中。只有在编译时,例如#if-defs,也就是这个#if-def必须排除main(){}从obj文件或准备链接error@FiddlingBits我认为有些库是使用
main()
-函数编译的,如果库作为程序执行,则该函数会打印使用信息。但是,如果二进制文件用作库,则其
main()
显然不会在启动时被调用。例如:gcc-DCOMPILE\u AS\u LIBRARY=1 source.c-DCOMPILE\u AS\u LIBRARY将从中排除main等compile@Olaf:然而,这正是我的libc6在Linux上所做的。