今天,在使用一个自定义库时,我发现了一种奇怪的行为.静态库代码包含调试main()
功能.它不在#define
旗帜内.所以它也存在于库中.它被用于链接到包含真实的另一个程序main()
.
当它们都链接在一起时,链接器不会抛出多个声明错误main()
.我想知道这是怎么发生的.
为简单起见,我创建了一个模拟相同行为的示例程序:
$ cat prog.c
#include <stdio.h>
int main()
{
printf("Main in prog.c\n");
}
$ cat static.c
#include <stdio.h>
int main()
{
printf("Main in static.c\n");
}
$ gcc -c static.c
$ ar rcs libstatic.a static.o
$ gcc prog.c -L. -lstatic -o 2main
$ gcc -L. -lstatic -o 1main
$ ./2main
Main in prog.c
$ ./1main
Main in static.c
Run Code Online (Sandbox Code Playgroud)
"2main"二进制文件如何找到main
要执行的内容?
但是将它们编译在一起会产生多重声明错误:
$ gcc prog.c static.o
static.o: In function `main':
static.c:(.text+0x0): …
Run Code Online (Sandbox Code Playgroud) 我在一个使用"make和gcc"编译所有模块的项目上工作.这些模块位于自己的文件夹中,并具有自己的Makefile.全局Makefile调用它们以编译二进制文件.
所以现在我尝试使用Visual Studio Code作为我的IDE.我已经设置了编译环境并且运行良好.
唯一的问题是,只要有一些警告/编译,点击它们就不会打开正确的文件.我的工作目录类似于下面显示的简化代码.
D:\SO
|-- common
| |-- main.c
| `-- Makefile
`-- Makefile
Run Code Online (Sandbox Code Playgroud)
从任务我将调用外部Makefile,它将调用Makefile内部常见.而在main.c中,我故意删除了stdio.h头文件包含,这应该显示一个隐式声明错误.但是,当我单击问题窗口上的警告时,VS代码会抛出错误,表明找不到该文件.VS Code尝试打开"D:\ SO\main.c",但文件实际上在"D:\ SO\common\main.c"中
外部Makefile
all:
(cd common && make )
Run Code Online (Sandbox Code Playgroud)
内部Makefile(公共目录内)
all:
gcc main.c
Run Code Online (Sandbox Code Playgroud)
main.c中
int main(int argc, char *argv[])
{
printf("Hello World");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "make",
"command": "make",
"type": "shell",
"problemMatcher": [
"$gcc"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我试图通过为fileLocation参数提供不同的组合来调整problemMatcher.但他们没有产生适当的结果.所以我没有把它包括在内.
我在Windows 10 1607 x64上使用Visual Studio …
我正在使用clang格式4.0.0来对齐我的个人项目。我将以下配置用于clang格式。
Language: Cpp
BreakBeforeBraces: Allman
ColumnLimit: 120
TabWidth: 4
IndentWidth: 4
UseTab: ForContinuationAndIndentation
Run Code Online (Sandbox Code Playgroud)
下面的示例代码使用上述配置进行了对齐。
struct test
{
int a;
int b;
int c;
};
struct test T = {
.a = 1, .b = 2, .c = 3,
};
Run Code Online (Sandbox Code Playgroud)
有什么方法可以像下面显示的那样对齐初始化部分。基本上,我正在寻找一种将所有初始化程序放在单独的行中的方法。
struct test T =
{
.a = 1,
.b = 2,
.c = 3,
};
Run Code Online (Sandbox Code Playgroud) 我有两组数据需要根据时间绘制。我需要单独显示它们,并借助单选按钮(或类似的方式)一起显示它们。单选按钮代码基于/sf/answers/468828881/
在加载第一组数据之前,一切看起来都很好。每当要绘制下一组数据时,我都会清除轴并重新绘制数据。但是当我单击单选按钮中的下一项时,会显示新数据,但 x 轴不会旋转。有没有什么办法解决这一问题。
重现我面临的问题的示例代码
import datetime
import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons
import matplotlib.dates as mdates
data0_usage = [45, 76, 20, 86, 79, 95, 14, 94, 59, 84]
data1_usage = [57, 79, 25, 28, 17, 46, 29, 52, 68, 92]
data0_timestamp = []
def draw_data_plot(ax, data_no):
if data_no == 0:
data_usage = data0_usage
data_color = 'go'
elif data_no == 1:
data_usage = data1_usage
data_color = 'ro'
ax.plot_date(data0_timestamp, data_usage, data_color)
ax.plot_date(data0_timestamp, data_usage, 'k', markersize=1)
def draw_plot():
fig …
Run Code Online (Sandbox Code Playgroud) 如果可以进行以下假设,是否可以恢复链表的头节点.
为了更好地说明,请使用以下程序,该程序可以在默认配置下至少在Ubuntu/WSL下使用gcc成功编译.
程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
typedef struct node
{
int val;
struct node *next;
} node_t;
node_t *head = NULL;
unsigned long start_address = 0;
unsigned long end_address = 0;
node_t *getLastNode()
{
node_t *iter = head;
for (; iter->next != NULL; iter = iter->next)
;
return iter;
}
void addToLinkedList(int value)
{
node_t *data = malloc(sizeof(node_t));
data->val = value;
data->next = NULL;
if (head == NULL)
head …
Run Code Online (Sandbox Code Playgroud) c ×2
gcc ×2
clang-format ×1
linked-list ×1
linux ×1
matplotlib ×1
pointers ×1
python ×1
vscode-tasks ×1