如何打印到c编译程序的地方的路径

时间:2022-02-08 22:53:59

I know we can print the path of the current working directory using something like getcwd() but if the program was compiled in one place and the executable was copied to some other place, it will give the result as the new directory.

我知道我们可以使用像getcwd()这样的东西打印当前工作目录的路径但是如果程序是在一个地方编译的并且可执行文件被复制到其他地方,它会将结果作为新目录。

How do we store the value of getcwd() or something during compilation itself?

我们如何在编译期间存储getcwd()或其他东西的值?

4 个解决方案

#1


3  

You could pass it as a compile-time define :

您可以将其作为编译时定义传递:

#include <stdio.h>

int main(void) {
    printf("COMPILE_DIR=%s\n", COMPILE_DIR);
    return 0;
}

And then :

接着 :

/dir1$ gcc -DCOMPILE_DIR=\"$(pwd)\" current.c -o current

Resulting in :

导致 :

/dir1$ ./current 
COMPILE_DIR=/dir1
/dir1$ cd /dir2
/dir2$ cp /dir1/current ./
/dir2$ ./current
COMPILE_DIR=/dir1

#2


2  

You can do this using makefiles, in makefile, add a macro that retain the pwd result:

您可以使用makefile在makefile中执行此操作,添加一个保留pwd结果的宏:

Makefile:

CFLAGS += -DCURRENT_DIR=\"$(shell pwd)\"

In c file, use this value:

在c文件中,使用此值:

#include <stdio.h>

int main(void)
{
    printf("file %s compiled from %s\n", __FILE__, CURRENT_DIR);
    return 0;
}    

#3


1  

If you use cmake in you build process, you can add add_definitions(-DSOME_DIR="${CMAKE_CURRENT_BINARY_DIR}") to a correct CMakeLists.txt file. That is equivalent to #define SOME_DIR "binaries_dir" in the code.

如果在构建过程中使用cmake,则可以将add_definitions(-DSOME_DIR =“$ {CMAKE_CURRENT_BINARY_DIR}”)添加到正确的CMakeLists.txt文件中。这相当于代码中的#define SOME_DIR“binaries_dir”。

Alternatively you can use any other build automation tool to make sure compiler gets passed the correct -D (GNU) or /D (MSVC) flag to generate a correct definition (or pass it to compiler manually, which is no different from specifying it in the code).

或者,您可以使用任何其他构建自动化工具来确保编译器传递正确的-D(GNU)或/ D(MSVC)标志以生成正确的定义(或手动将其传递给编译器,这与指定它无异。代码)。

#4


0  

Depending on your compiler this may work:

根据您的编译器,这可能有效:

#include<stdio.h>

int main()
{
  printf("This file path: %s\n", __FILE__);
}

__FILE__ is a predefined string literal that contains the full path of the .c file at compilation time.

__FILE__是一个预定义的字符串文字,包含编译时.c文件的完整路径。

If you want just the directory, you need to can strip off the filename yourself by a some trivial string manipulations.

如果你只想要这个目录,你需要通过一些简单的字符串操作来自己去除文件名。

This works under Visual Studio 2017. I didn't test it with other platforms. Apparently on most other platforms __FILE__ just contains the filename without the path.

这适用于Visual Studio 2017.我没有用其他平台测试它。显然在大多数其他平台上__FILE__只包含没有路径的文件名。

#1


3  

You could pass it as a compile-time define :

您可以将其作为编译时定义传递:

#include <stdio.h>

int main(void) {
    printf("COMPILE_DIR=%s\n", COMPILE_DIR);
    return 0;
}

And then :

接着 :

/dir1$ gcc -DCOMPILE_DIR=\"$(pwd)\" current.c -o current

Resulting in :

导致 :

/dir1$ ./current 
COMPILE_DIR=/dir1
/dir1$ cd /dir2
/dir2$ cp /dir1/current ./
/dir2$ ./current
COMPILE_DIR=/dir1

#2


2  

You can do this using makefiles, in makefile, add a macro that retain the pwd result:

您可以使用makefile在makefile中执行此操作,添加一个保留pwd结果的宏:

Makefile:

CFLAGS += -DCURRENT_DIR=\"$(shell pwd)\"

In c file, use this value:

在c文件中,使用此值:

#include <stdio.h>

int main(void)
{
    printf("file %s compiled from %s\n", __FILE__, CURRENT_DIR);
    return 0;
}    

#3


1  

If you use cmake in you build process, you can add add_definitions(-DSOME_DIR="${CMAKE_CURRENT_BINARY_DIR}") to a correct CMakeLists.txt file. That is equivalent to #define SOME_DIR "binaries_dir" in the code.

如果在构建过程中使用cmake,则可以将add_definitions(-DSOME_DIR =“$ {CMAKE_CURRENT_BINARY_DIR}”)添加到正确的CMakeLists.txt文件中。这相当于代码中的#define SOME_DIR“binaries_dir”。

Alternatively you can use any other build automation tool to make sure compiler gets passed the correct -D (GNU) or /D (MSVC) flag to generate a correct definition (or pass it to compiler manually, which is no different from specifying it in the code).

或者,您可以使用任何其他构建自动化工具来确保编译器传递正确的-D(GNU)或/ D(MSVC)标志以生成正确的定义(或手动将其传递给编译器,这与指定它无异。代码)。

#4


0  

Depending on your compiler this may work:

根据您的编译器,这可能有效:

#include<stdio.h>

int main()
{
  printf("This file path: %s\n", __FILE__);
}

__FILE__ is a predefined string literal that contains the full path of the .c file at compilation time.

__FILE__是一个预定义的字符串文字,包含编译时.c文件的完整路径。

If you want just the directory, you need to can strip off the filename yourself by a some trivial string manipulations.

如果你只想要这个目录,你需要通过一些简单的字符串操作来自己去除文件名。

This works under Visual Studio 2017. I didn't test it with other platforms. Apparently on most other platforms __FILE__ just contains the filename without the path.

这适用于Visual Studio 2017.我没有用其他平台测试它。显然在大多数其他平台上__FILE__只包含没有路径的文件名。