views:

444

answers:

3

I am learning C and because VC++ 2008 doesn't support C99 features I have just installed NetBeans and configure it to work with MinGW. I can compile single file project ( main.c) and use debugger but when I add new file to project I get error "undefined reference to ... function(code) in that file..". Obviously MinGW does't link my files or I don't know how properly add them to my project (c standard library files work fine).

/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/Users/don/Documents/NetBeansProjects/CppApplication_7'
/bin/make  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/cppapplication_7.exe
make[2]: Entering directory `/c/Users/don/Documents/NetBeansProjects/CppApplication_7'
mkdir -p dist/Debug/MinGW-Windows
gcc.exe     -o dist/Debug/MinGW-Windows/cppapplication_7 build/Debug/MinGW-Windows/main.o  
build/Debug/MinGW-Windows/main.o: In function `main':
C:/Users/don/Documents/NetBeansProjects/CppApplication_7/main.c:5: undefined reference to `X'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW-Windows/cppapplication_7.exe] Error 1
make[2]: Leaving directory `/c/Users/don/Documents/NetBeansProjects/CppApplication_7'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/c/Users/don/Documents/NetBeansProjects/CppApplication_7'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)

main.c

#include "header.h"

int main(int argc, char** argv)
{
    X();
    return (EXIT_SUCCESS);
}

header.h

#ifndef _HEADER_H
#define _HEADER_H
#include <stdio.h>
#include <stdlib.h>

void X(void);

#endif

source.c

#include "header.h"
void X(void)
{
    printf("dsfdas");
}
A: 

This content edited into the question by Bill.

dontoo
You should edit the question, not answer it.
Billy ONeal
@Billy: Not with only 74 rep he shouldn't.
Donal Fellows
@Donal Fellows: You can always comment on your own question. You can also always comment on answers to your own question. For example, he commented on my answer. Same deal goes for editing your own questions -- you can always do that.
Billy ONeal
Missed that. Too many meetings...
Donal Fellows
A: 

Try changing the name of your include guards

#ifndef _HEADER_H //These
#define _HEADER_H
#include <stdio.h>
#include <stdlib.h>

void X(void);

#endif

Names beginning with an underscore (_) are reserved for use by the C and C++ standard libraries. It's entirely possible _HEADER_H is already defined somewhere, which would make main.c not compile.

Billy ONeal
That's not the issue. When I have for example main.c file and source.c file, and I include source.c in main.c everything works. But when I have main.c->include->header.h and source.c->include header.h I get error "undefined reference to function `X'"
dontoo
obviously I don't know how to add files correctly
dontoo
A: 

I found what was wrong. I was adding files in physical view not while I am in logical view.

dontoo