Reputation: 9592
I did read all the other posts unable to find a conclusive answer.N.B. I know what linker error is. Here is my file. I'm trying to compile it as gcc -o sapy test_assign1_1.c Assignment1.c
and getting error
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)
I just want to know the minimum command that should I type to make this program compile. A bonus would be to know how can I set it up on 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();
}
Upvotes: 0
Views: 225
Reputation: 21
so imagine you have a program on c called "test.c" like this one:
#include "a.h"
#include "b.h"
#include "c.h"
int main(void){
// do something
}
Then, You will have a folder like this
|
|--test.c
|--a.c
|--a.h
|--b.c
|--b.h
|--c.h
In order to compile this, you should just write:
gcc -o test test.c a.c b.c c.h
Upvotes: 0
Reputation: 175
As the best practice goes, you need to have the corresponding .c
files for each of your custom headers.
gcc -o sapy storage_mgr.c dberror.c test_helper.c
If you do not want to expose the source code of your function defined in the custom headers, you can use
gcc -c storage_mgr.c dberror.c test_helper.c
to get the object (.o
) files and get the final executable by compiling the object files together as
gcc -o sapy storage_mgr.o dberror.o test_helper.o
Upvotes: 1