Beginner - C project - trouble compiling - Main.c:(.text+0xa): undefined reference to


#1

Hello quite noobish here. I am taking a C (not c++) course for engineering. I am trying to get cloud9 set up so that my partner and I can collaborate easily and code from the browser and not have to be booting into Ubuntu constantly to code.

I have the following project, has main.c, StockAnalyzer.c, StockAnalyzer.h, test.c (for testing StockAnalyzer.c), and “yahoo.txt” (a sample data text file with tab delimited data).

I have copied all my code from my existing project in Eclipse on Ubuntu. When I “run as simple c” main.c, I get the error:Main.c:(.text+0xa): undefined reference to GetStockTest(). GetStockTest() is prototyped at the beginning of main.c, and is found in test.c.

It seems that cloud9 can’t find this function. In Eclipse I didn’t have to include any files in main.c to make this work, the prototype was enough.

Anyone can help? Thanks.


#2

Try removing the function headers that are above main. They should be defined in a .h file and implemented inside a .cpp file

EDIT .cop => .cpp file. My phone automatically changed it.


#3

I’m not even aware of what a .cop file is. Like I said, it’s an entry level C course. We are to use what we’ve learned.

The code compiles in Eclipse, no .cop files. Is that really the standard? .cop files?

The .h header file just replaces the #include call with the contents of the .h file correct? So there should be no difference between putting prototypes in the main.c and #include a header file with the exact same text.


I wish I could find a tutorial where they compiled C in Cloud9, preferably with multiple files like I have and not just a single main.c.


#4

Sorry. My last reply had a typo, my phone’s autocorrect changed .cpp into .cop . Also try reading this link, it describes how to use header files correctly.

And more specific information for the C language
https://www.tutorialspoint.com/cprogramming/c_header_files.htm


#5

Thanks. This is a C project, not C++ as stated in the OP. So .c file is correct. And the files compile just fine in Eclipse. I have it working no problem in Eclipse. I am looking to get an already working project into Cloud9 so group members can collaborate.

Anyone else can help?


#6

As I understand: The simple c runner assumes your project will have only one source code file. So it compiles the single file that is loaded when you press run.

One workaround for multiple source code files (*.c or *.cpp) is to set up a makefile to handle the compiling and linking. This is also useful if you have other libraries to link against.

Then you can create a custom runner to call your makefile instead of the basic gcc call. I found the following discussion helpful when I first got started

The runner I set up uses the following code (hopefully this will be helpful to others).

// Create a custom Cloud9 runner - similar to the Sublime build system
// For more information see https://docs.c9.io/custom_runners.html
{
    "script" : [
        "set -e",
        "if [ \"$debug\" == true ]; then ",
            "make -f $file_path/Makefile debug",
            "chmod 755 \"$file.o\"",
            "node $HOME/.c9/bin/c9gdbshim.js \"$file.o\" $args",
        "else",
            "make -f $file_path/Makefile",
            "chmod 755 $file.o",
            "$file.o $args",
        "fi"
    ],
    "info": "Running $file",

    "debugger": "gdb",
    "$debugDefaultState": false,

    "env" : {},
    "selector": "^.*\\.(cpp|cc)$"
}

Note for the debug make target, I set it to add the following flags (for g++) -ggdb3 -o0 which I had seen suggested elsewhere.


#7

I seriously appreciate you guys responding and trying to help me get this working.

It looks like cloud9 is over my head (no pun). Makefiles are not something I’ve touched on, and compiling from the command line is not something I have experience with either. I may give it another try in the future.

Cloud9 looked like the best bet for what I was looking for. But it looks like I’ll have more trouble working around cloud9 than I will using cloud9 to assist in saving time and hassle.