I´m using Visual C++ Studio and when I include another .cpp file it doesn´t takes all the what have been included before.

Here is an example:

main.cpp

#include <iostream>
#include <string>
#include <sstream>
#include "readtext.cpp"

int main()
{
 teste();
return 0;
}

//readtext.cpp
void text()
{
	printf("teste");
}

It returns the error;

readtext: : error C3861: 'printf': identifier not found

That is because you have not #include <cstdio> Also, you should pretty much never include a .cpp file.

I know that, but I´m new at Visual Studio, so I was trying loads of solutions, this was one of them, I think I´ve copied the worse one, LOL.

Still doesnt work. same error:

error C3861: 'printf': identifier not found

This compiles fine for me:

#include <iostream>
#include <cstdio>

int main()
{
 printf("teste");
 return 0;
}

Dave

Let me describe better my problem.

I created a new empty project on MS Visual C++.

Then I created two .cpp files inside it.

Something like that:

/* file1.cpp */

#include <iostream>
#include <cstdio>

int main()
{
 printf("teste");
 functionX();
 return 0;
}

/* end of file1.cpp */

/* file2.cpp */

int functionX()
{  
   printf("Another test");
   return 0;
}

It returns this error;
file2.cpp(3) : error C3861: 'printf': identifier not found

So the includes of the file1.cpp are not being used, or included in file2.cpp

Does anyone knows how to solve this problem?

Thanks for the support.

Fernando

Just #include <cstdio> in file2.cpp.

If I remove #include file2.cpp it returns the error, that it´s not supposed to be used of course.
1>------ Build started: Project: testing includes, Configuration: Debug Win32 ------
1>Compiling...
1>file1.cpp
1>c:\users\fernando\documents\visual studio 2008\projects\testing includes\testing includes\file1.cpp(7) : error C3861: 'functionX': identifier not found
1>files2.cpp
1>Generating Code...
1>Build log was saved at "file://c:\Users\Fernando\Documents\Visual Studio 2008\Projects\testing includes\testing includes\Debug\BuildLog.htm"
1>testing includes - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

If I include file2.cpp like #include "file2.cpp"

it returns the error;
1>------ Build started: Project: testing includes, Configuration: Debug Win32 ------
1>Compiling...
1>file1.cpp
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation. All rights reserved.
1>Linking...
1>files2.obj : error LNK2005: "int __cdecl functionX(void)" (?functionX@@YAHXZ) already defined in file1.obj
1>C:\Users\Fernando\Documents\Visual Studio 2008\Projects\testing includes\Debug\testing includes.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Users\Fernando\Documents\Visual Studio 2008\Projects\testing includes\testing includes\Debug\BuildLog.htm"
1>testing includes - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


So gess while building the compiler is not linking file1 with file 2 that´s why the error is happening. do you know how to do that?

You absolutely should not #include "file2.cpp" . Since you're using visual studio the linking should be setup automatically for you - but I'll let a VS user reply with the details.

Ok thank you for the support, I´m new on VS so I´m still fighting with it, lol.

Please post

File1.cpp

//file1.cpp here

File2.cpp

//file2.cpp here

So the next guy knows where you are so far.

Dave

Here we go, both file are in the same solution of course.

File1.cpp

#include <iostream>
#include <cstdio>


int main()
{
 printf("teste");
 functionX();
 return 0;
}

File2.cpp

#include <cstdio>

int functionX()
{  
   printf("Another test");
   return 0;
}

Visual Studio returns the error:

1>------ Build started: Project: testing includes, Configuration: Debug Win32 ------
1>Compiling...
1>file1.cpp
1>c:\users\fernando\documents\visual studio 2008\projects\testing includes\testing includes\file1.cpp(8) : error C3861: 'functionX': identifier not found
1>Build log was saved at "file://c:\Users\Fernando\Documents\Visual Studio 2008\Projects\testing includes\testing includes\Debug\BuildLog.htm"
1>testing includes - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

As you can see, VS onde compiles file1.cpp =/ .

Fernando.

Best practice would be to create a File2.h which defines functionX()& include it:

#include <iostream>
#include <cstdio>
#include "File2.h" //<--
 
 
int main()
{
 printf("teste");
 functionX();
 return 0;
}

else include File2.cpp instead of File2.h there.

Nisheeth Barthwal,

Thank you! That works!!

=) now it included file2.cpp while compiling =)

Thanks Dave and Nisheeth!

Best regards,

Fernando.

Glad to be of help, mark this thread Solved please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.