문제

I wrote a c program in Visual C++.

test2.cpp

    #pragma startup pragmaEgFun1
    void pragmaEgFun1(){
    printf("Hello 1");
    }

test1.cpp

    #include "test2.cpp"
    int main{
    printf("Hello 2");
    }

This program was to test pragma preprocessor directive. But i am getting an error Error

error LNK2005: "void __cdecl pragmaEgFun1(void)" (?pragmaEgFun1@@YAXXZ) already defined in test1.obj

도움이 되었습니까?

해결책

There are three ways to solve this:

  1. Simply remove the #include line from test1.cpp. Since you are not calling that function, you don't need to include it anyway. The pragma startup should take care that the function is called and it will be available because VC will link it.

  2. Rename test2.cpp to test2.h and include that one in test1.cpp. This would be not a best practice though, because functions are normally not supposed to be in header files, even though it is allowed by the compiler.

  3. You can place the function from test2.cpp directly into test1.cpp, as you don't really need it anyway.

To answer your comment - normally you put functions into .c/.cpp files and declarations, prototypes, definitions, macros etc. into the .h files. This is not dictated by the compiler though, but good coding practices help to avoid such problems as you posted in your question.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top