gpt4 book ai didi

c - 如何编译带有头文件的c文件

转载 作者:行者123 更新时间:2023-11-30 14:35:59 24 4
gpt4 key购买 nike

我确实阅读了所有其他帖子,但找不到结论性的答案。我知道什么是链接器错误。这是我的文件。我试图将其编译为 gcc -o sapy test_assign1_1.cAssignment1.c 并收到错误

Undefined symbols for architecture x86_64:
"_errorMessage", referenced from:
_testCreateOpenClose in test_assign1_1-506ae6.o
_testSinglePageContent in test_assign1_1-506ae6.o
_additionalTestCases in test_assign1_1-506ae6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我只是想知道我应该输入的最少命令以使该程序编译。一个额外的好处是知道如何在 VScode 上设置它。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "storage_mgr.h"
#include "dberror.h"
#include "test_helper.h"

// test name
char *testName;

/* test output files */
#define PAGEFILETEST "test_pagefile.bin"

/* prototypes for test functions */
static void testCreateOpenClose(void);
static void testSinglePageContent(void);
static void additionalTestCases(void);

/* main function running all tests */
int
main (void)
{
testName = "";

initStorageManager();
testCreateOpenClose();
testSinglePageContent();
additionalTestCases();
return 0;
}


/* check a return code. If it is not RC_OK then output a message, error description, and exit */
/* Try to create, open, and close a page file */
void
testCreateOpenClose(void)
{
SM_FileHandle fh;
//SM_PageHandle ph = (SM_PageHandle) malloc(PAGE_SIZE);
testName = "test create open and close methods";

TEST_CHECK(createPageFile (PAGEFILETEST));
printf(" Page file created");
TEST_CHECK(openPageFile (PAGEFILETEST, &fh));
ASSERT_TRUE(strcmp(fh.fileName, PAGEFILETEST) == 0, "filename correct");
ASSERT_TRUE((fh.totalNumPages == 1), "expect 1 page in new file");
ASSERT_TRUE((fh.curPagePos == 0), "freshly opened file's page position should be 0");

TEST_CHECK(closePageFile (&fh));
TEST_CHECK(destroyPageFile (PAGEFILETEST));
// after destruction trying to open the file should cause an error
ASSERT_TRUE((openPageFile(PAGEFILETEST, &fh) != RC_OK), "opening non-existing file should return an error.");
// memset(ph, 0, PAGE_SIZE);
//TEST_CHECK(writeCurrentBlock(&fh,ph))
TEST_DONE();
}

/* Try to create, open, and close a page file */
void
testSinglePageContent(void)
{
SM_FileHandle fh;
SM_PageHandle ph;
int i;

testName = "test single page content";


ph = (SM_PageHandle) malloc(PAGE_SIZE);

// create a new page file
TEST_CHECK(createPageFile (PAGEFILETEST));
TEST_CHECK(openPageFile (PAGEFILETEST, &fh));

// read first page into handle
TEST_CHECK(readFirstBlock (&fh, ph));
// the page should be empty (zero bytes)
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page");
printf("first block was empty\n");

// change ph to be a string and write that one to disk
for (i=0; i < PAGE_SIZE; i++)
ph[i] = (i % 10) + '0';
TEST_CHECK(writeBlock (0, &fh, ph));
printf("writing first block\n");

// read back the page containing the string and check that it is correct
TEST_CHECK(readFirstBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "character in page read from disk is the one we expected.");
printf("reading first block\n");
TEST_CHECK(writeCurrentBlock(&fh,ph));
// printf("run writeCurrentBlock with 1...\n");
// destroy new page file
TEST_CHECK(destroyPageFile (PAGEFILETEST));

TEST_DONE();
}

void additionalTestCases(void){
SM_FileHandle fh;
SM_PageHandle ph;
int i;

testName = "test multiple page content";


ph = (SM_PageHandle) malloc(PAGE_SIZE);

// create a new page file
TEST_CHECK(createPageFile (PAGEFILETEST));
TEST_CHECK(openPageFile (PAGEFILETEST, &fh));

// read first page into handle
TEST_CHECK(readFirstBlock (&fh, ph));
// the page should be empty (zero bytes)
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page");
printf("first block was empty\n");

// change ph to be a string and write that one to disk
for (i=0; i < PAGE_SIZE; i++)
ph[i] = (i % 10) + '0';
TEST_CHECK(writeBlock (0, &fh, ph));
printf("writing first block\n");

// read back the page containing the string and check that it is correct
TEST_CHECK(readFirstBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "character in page read from disk is the one we expected.");
printf("reading first block\n");
TEST_CHECK(writeCurrentBlock(&fh,ph));


// Reading the next block from file.
TEST_CHECK(readNextBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "Expecting character in page read from disk.");
printf("Reading next block \n");

// Reading the current block from file.
TEST_CHECK(readCurrentBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "Expecting character in page read from disk.");
TEST_CHECK(readPreviousBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "Expecting character in page read from disk.");
printf("Reading previous block \n");


// Reading the specific block (2nd block in this case) from file.
TEST_CHECK(readBlock(2,&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "Expecting character in page read from disk.");
printf("Reading second block \n");

// Reading the last block from file.
TEST_CHECK(readLastBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "Expecting character in page read from disk.");
printf("Reading last block \n");

// destroy new page file
TEST_CHECK(destroyPageFile (PAGEFILETEST));

TEST_DONE();
}

最佳答案

按照最佳实践,您需要为每个自定义 header 提供相应的 .c 文件。

gcc -o sapy storage_mgr.c dberror.c test_helper.c

如果您不想公开自定义 header 中定义的函数的源代码,可以使用

gcc -c storage_mgr.c dberror.c test_helper.c

获取对象 (.o) 文件并通过将对象文件编译在一起来获取最终的可执行文件

gcc -o sapy storage_mgr.o dberror.o test_helper.o

关于c - 如何编译带有头文件的c文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58262686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com