I've just been beaten (rather hardly) on the head by some non-trivial warning from Visual Studio 2010 (C++).
The compilation gave the following output:
1 Debug\is.obj : warning LNK4042: object specified more than once; extras ignored
1 Debug\make.obj : warning LNK4042: object specified more than once; extras ignored
1 Debug\view.obj : warning LNK4042: object specified more than once; extras ignored
1 identity.obj : error LNK2019: unresolved external symbol void __cdecl
test::identity::view(void) (?view#identity#test##YAXXZ) referenced in function void __cdecl test::identity::identity(void) (?identity#0test##YAXXZ)
1 identity.obj : error LNK2019: unresolved external symbol void __cdecl test::identity::make(void) (?make#identity#test##YAXXZ) referenced in function void __cdecl test::identity::identity(void) (?identity#0test##YAXXZ)
1 range.obj : error LNK2019: unresolved external symbol void __cdecl test::range::is(void) (?is#range#test##YAXXZ) referenced in function void __cdecl test::range::range(void) (?range#0test##YAXXZ)
Linker errors are always a pain to debug... but there were unresolved references, and so I checked... but the source is well-formed... and finally it hit me:
My folder hierarchy looks like so:
src/
identity/
is.cpp
make.cpp
view.cpp
range/
is.cpp
make.cpp
view.cpp
and so does the hierarchy in the Solution (I always set it up so that it mimicks the "real" folder structure).
And the diagnostic outputs:
Debug\is.obj
Debug\make.obj
Debug\view.obj
Along with a warning which says that the .obj has been passed twice to the linker and that one will be ignored.
Search no more: Visual has neatly flatten my folder hierarchy, and therefore is unable to neatly compile the source.
At the moment, I am simply thinking of renaming the files, that should cover the issue...
... but is there a way to have Visual Studio NOT flatten the file hierarchy ?
I had a similar problem with linker warning LNK4042: object specified more than once; extras ignored. In my case Visual Studio was trying to compile both header and source files with the same name - MyClass.h and MyClass.cpp. It happened because I renamed .cpp file to .h and Visual Studio got confused. I noticed the problem by looking at the compiler logs in the Debug directory. To resolve just remove .h file from the project then add it again.
Just wanted to cross post what I believe to be the answer, if you open the properties for the entire project, and the change the value under C/C++ -> Output Files -> "Object File Name" to be the following:
$(IntDir)/%(RelativeDir)/
Under VS 2010, I believe this will disambiguate all of the object files (as I believe windows won't let you under any crazy circumstances have two files with the same names in the same directory). Please also check out the details here.
Right-click the .cpp file in the Solution Explorer window, Properties, C/C++, Output Files, Object File Name setting. The default is $(IntDir)\, that's what is doing the flattening. All the .obj file will go into $(IntDir), the "Debug" directory in the debug configuration.
You can change the setting, say $(IntDir)\is2.obj. Or select all the files from one group (use Shift+Click) and change the setting to, say, $(IntDir)\identity\
Or you can change the .cpp filename so that .obj files don't overwrite each other. Having files with the exact same name in two directories is a bit odd.
Or you can create multiple projects, creating, say, .lib projects for the files in identity and range. Commonly done in makefile projects for example. That does however make managing the compile and link settings more of a hassle unless you use project property sheets.
Right click on header file -> Property -> ItemType (select C/C++ Header). Do the same with Cpp file but select C/C++ Compiler (it's work for me)
Alternatively to deleting and making a new file you can change the compile/include settings.
Go to your project.vcxproj file, open it with an editor, find the html like line <ItemGroup>.
It should look something like:
<ItemGroup>
<ClCompile Include="implementation.cpp" />
</ItemGroup>
and
<ItemGroup>
<ClInclude Include="declaration.hpp" />
</ItemGroup>`
Assuming your implementation files are .cpp and your declarations are .hpp. Make sure your all your implementation files are listed between the first section if you have more then one and likewise for the second section for multiple declaration files.
I had this problem with stdafx.cpp. Somehow stdafx.cpp got duplicated, so there was a second StdAfx.cpp (mind the different case).
After I removed the StdAfx.cpp everything worked fine!
Using VS 2010.
I use $(IntDir)\%(Directory)\ under C/C++ -> Output Files -> "Object File Name".
I used to have in the same project .c and .cpp files with the same filenames. The files were in folders all over the place and the solutions provided by others created a mess, and folder hell (in my case). Even Release builds would overwrite Debug builds!
A good (not perfect) solution would be to use $(ParentName), but for some reason beyond anyone's grasp it has been removed from later versions of Visual Studio (2015+).
What I use succesfully now is:
$(IntDir)%(Filename)%(Extension).obj
which at least separates .c built object files from .cpp.
I'd like to point out one possible reason for why the ItemType of a .h file would change from C/C++ header to C/C++ compiler:
In the Solution Explorer window of VS (2019 here), right click the project name, choose Add -> New Item;
Select the C++ File (.cpp) template, but type something.h in the name input area, then click OK to add it;
Then you'll encounter the LNK4042 warning if the something.h file be included within more than one .cpp files.
I just overcame a similar error message, and lots more with the procedure below. Symptom: one linker error for every invocation of every function defined in a particular header, plus one at the end of output for every function defined in the header.
Then I remembered that when I had originally created this header, I accidentally had selected "add->new item->c++ file" and though I named it 'whatever.h', it seems Visual Studio considered them both the same kinds of files because of the incorrect action I used to add one. Examining the build output logs made this obvious.
SOLUTION (Using VS Community 2019)
Back up project first (just to be safe).
Right-click the offending header file and select "Exclude from project" (this will not delete them; the VS project will just ignore them).
Do same for the matching .c or .cpp file.
Do Build->Clean on project
Do Build->Rebuild on project
-- there of course will be errors---
Right-click Header Files->Add->Existing Item, then select the .h file
Right-click Source Files->Add->Existing Item, the select the .c or .cpp file
Do Build->Rebuild on project.
This completely cleaned it up for me, relieving me of many irritating linker errors including LNK4042 from the title of this question.
I resolved it changing filenames in my project. There was two files named main.c and main.cpp. I changed one of them and worked.
Related
When I build my c++ solution in Visual Studio it complains that the xxxxx.pch file is missing. Is there a setting I am missing to get the pre-compiled headers back?
here is the exact error for completeness:
Error 1 fatal error C1083: Cannot open precompiled header file: 'Debug\xxxxx.pch': No such file or directory
NOTE: Later versions of the IDE may use "pch" rather than "stdafx" in the default names for related files. It may be necessary to substitute pch for stdafx in the instructions below. I apologize. It's not my fault.
Right-click on your project in the Solution Explorer.
Click Properties at the bottom of the drop-down menu.
At the top left of the Properties Pages,
select All Configurations from the drop-down menu.
Open the C/C++ tree and select Precompiled Headers
Precompiled Header: Select Use (/Yu)
Fill in the Precompiled Header File field. Standard is stdafx.h
Click Okay
If you do not have stdafx.h in your Header Files put it there. Edit
it to #include all the headers you want precompiled.
Put a file named stdafx.cpp into your project. Put #include "stdafx.h"
at the top of it, and nothing else.
Right-click on stdafx.cpp in Solution Explorer. Select Properties
and All configurations again as in step 4 ...
... but this time select Precompiled Header Create (/Yc). This will only
bind to the one file stdafx.cpp.
Put #include "stdafx.h" at the very top of all your source files.
Lucky 13. Cross your fingers and hit Build.
Precompiled Header (pch) use is a two-step process.
In step one, you compile a stub file (In VS200x it's usually called stdafx.cpp. Newer versions use pch.cpp.). This stub file indirectly includes only the headers you want precompiled. Typically, one small header (usually stdafx.h or pch.hpp) lists standard headers such as <iostream> and <string>, and this is then included in the stub file. Compiling this creates the .pch file.
In step 2, your actual source code includes the same small header from step 1 as the first header. The compiler, when it encounters this special header, reads the corresponding .pch file instead. That means it doesn't have to (re)compile those standard headers every time.
In your case, it seems step 1 fails. Is the stub file still present? In your case, that would probably be xxxxx.cpp. It must be a file that's compiled with /Yc:xxxxx.pch, since that's the compiler flag to indicate it's step 1 of the PCH process. If xxxxx.cpp is present, and is such a stub file, then it's probably missing its /Yc: compiler option.
Fix:
Make sure you have xxxxx.cpp in your project
Compile xxxxx.cpp with /Yc flag (Create Precompiled Header)
(right click on xxxxx.cpp -> properties -> Precompiled Headers -> create)
Compile all other files with /Yu flag (Use Precompiled Header)
(right click on project -> properties -> Precompiled Headers -> use)
Right click to the project and select the property menu item
goto C/C++ -> Precompiled Headers
Select Not Using Precompiled Headers
Yes it can be eliminated with the /Yc options like others have pointed out but most likely you wouldn't need to touch it to fix it. Why are you getting this error in the first place without changing any settings? You might have 'cleaned' the project and than try to compile a single cpp file. You would get this error in that case because the precompiler header is now missing. Just build the whole project (even if unsuccessful) and than build any single cpp file and you won't get this error.
In case this is happening to you on a server build (AppCenter) and yo uaer using CocoaPods ensure that your Podfile is checked in.
AppCenter only runs the "pod install" command if it finds a Pofile and it DOES NOT find the PODS folder on the files.
I had the folder checked-in, but because git automatically ignores .pch files (check you .gitignore to veryfy this), my .pch weren'nt being checked in.
I sorted my issue by forcing the .pch files to check it, but Deleting the PODS folder should work too, since Appcenter will run the pod install command in that case.
Hoppefully this helps somebody.
VS screwed (mine is 2019 ;( ).
Go ahead and choose "not using precompiled headers" as other guys are pointing out then open the project file (vcxproj) with any text editor, and delete the outlined two entries in two places. Enjoy cleaning up the mess!
As a matter of fact, the 'pch.h' entry in the vcxproj file you see it below, you will ever find it in VS properties' interfaces.
Try Build > Clean Solution, then Build > Build Solution. This works for me.
I know this topic is very old, but I was dealing with this in VS2015 recently and what helped was to deleted the build folders and re-build it. This may have happen due to trying to close the program or a program halting/freezing VS while building.
I was searching for the iOS PCH file having the same problem, if you got here like me too, the solution that I've found is by clearing derived data; Close Simulator(s), go to xCode prefs -> locations -> go to the derived data file path, close xCode, delete the files in the derived data folder, re launch and cheers :)
I managed to create this problem for myself because I wanted to use a pch.h and pch.cpp file from different directories. So, I deleted the two files from my project and then added them as existing files from somewhere else. Big mistake as precompiled header files can no longer be found.
There is no way that I can find to fix the problem from the Visual Studio 2019 UI. You must edit the project file and make sure the following look like this:
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
I had same issue, and I managed to solve it like this:
ERROR :
fatal error C1083: Cannot open precompiled header file : "Debug\myProj.pch". No such file or directory
first one is when I had an error,
and changed it like a second picture
make (/Yx)
myProj.h
In my case, it was necessary to select Create (/Yu), instead of the standard Use (/Yu)
to
If everything is right, but this mistake is present, it need check next section in ****.vcxproj file:
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition=
In my case it there was an incorrect name of a configuration: only first word.
I have a C++ .h and .cpp file from another project that I want to include into my project.
I don't want to copy the files over into my project since I want any changes to those files be applied to both projects.
I've included the directory of the file's folder in the
Properties->VC++ Directories->Include Directories
I've also included the folder in the
Properties->C/C++ -> General -> Additional Include Directories
The .h files seem to work. If I rename the include to anything other than
#include "myfile.h"
The cpp file gets unknown definitions.
When I compile. The error is
fatal error C1083: Cannot open source file: '..\..\..\..\..\..\my project\myfile.cpp': No such file or directory
If I remove the cpp file from the project. Then I get a long list of unresolved functions.
error LNK2019: unresolved external symbol "public: unsigned long __thiscall myclass::myfunction"
How can I include both the .h and .cpp file into my second project?
For cpp files you can just use right mouse click on project, select "add"->existing item.
Then they should compile with others, when a build initiated.
Slightly more complicated with headers. There is two ways to use #include directive:
with < > and " " braces
When " " braces used, compiler treats path as relative (if not absolute used) to location of cpp file.
When < > braces used, compiler looks for file in something like system include folders. For example - stdlib headers folder and windows.h location folder. Properties entry Additional Include Directories also goes there.
I suggest you to change projects structure and extract shared features from both projects to compile it as static library. Place shared headers in some subfolder inside library project and refer then as
#include "mylibHeaderDir/someheader.h"
In dependent projects, after setting Additional Include Directories you can refer theese includes as
#include <myLibHeaderDir/someheader.h>
This approach will help you in future, as you can reuse that shared module in every project you want.
About how to create and link static library you can read this article http://msdn.microsoft.com/en-us/library/vstudio/ms235627(v=vs.110).aspx Version of visual studio may be differ, but basics are the same.
You can't just pick files like that. There are two reasonable ways to solve it. 1, shared the file by means of a Code Versioning System (e.g. svn/git). 2, compile the the .cpp into a library and link to that library.
If the cpp can be used by multiple projects, it must mean that the code is something common. That means you should compile that code by itself into a library and then share that library. Compiling the same cpp into multiple libraries is likely to result in conflicts later if two such libraries are ever needed to work together.
Try to drag them into your solution?
You can create a new folder in your solution, and drag them all into this folder!
This question is similar to the one I asked yesterday
https://stackoverflow.com/questions/24332513/error-lnk2019-fatal-error-lnk1120?noredirect=1#comment37614189_24332513
That was answered very comprehensively (even though it turned out to be duplicate).
To solve round the problem I simply added the missing files to the header files and source files in the project explorer using
add existing item...
(I am using visual studio 2008).
However I would like to be able to add these files in a different way by using:
Project properties->General->Include Additional Directories
And adding the location where BinModel01.h and BinModel01.cpp are stored.
but unfortunately this does not seem to work?
(Obviously for one or two files it doesn't make a lot of difference but for bigger projects it would quickly become a pain!)
I get the same errors that I was getting before? (In fact this was the configuration that caused the original errors).
I am certain that I am adding the correct "Additional Directories"?
So do I have to do anything else other than add the additional directories? (it would seem so).
Thanks
Baz
LNK2019:
http://msdn.microsoft.com/en-us/library/799kze2z.aspx
From this site:
"unresolved external symbol 'symbol' referenced in function 'function'"
This means that you are using a function or variable that is declared but not defined. So you see the .h file, but not the .cpp. In your situation it likely means you haven't added the .cpp files to the project, or you are not compiling them.
You can't add a whole folder to your project, you need to add each file you need (but you can add more files at once).
"Additional Include Directories" means folders where VS will look in for headers in #include statements.
I've just been beaten (rather hardly) on the head by some non-trivial warning from Visual Studio 2010 (C++).
The compilation gave the following output:
1 Debug\is.obj : warning LNK4042: object specified more than once; extras ignored
1 Debug\make.obj : warning LNK4042: object specified more than once; extras ignored
1 Debug\view.obj : warning LNK4042: object specified more than once; extras ignored
1 identity.obj : error LNK2019: unresolved external symbol void __cdecl
test::identity::view(void) (?view#identity#test##YAXXZ) referenced in function void __cdecl test::identity::identity(void) (?identity#0test##YAXXZ)
1 identity.obj : error LNK2019: unresolved external symbol void __cdecl test::identity::make(void) (?make#identity#test##YAXXZ) referenced in function void __cdecl test::identity::identity(void) (?identity#0test##YAXXZ)
1 range.obj : error LNK2019: unresolved external symbol void __cdecl test::range::is(void) (?is#range#test##YAXXZ) referenced in function void __cdecl test::range::range(void) (?range#0test##YAXXZ)
Linker errors are always a pain to debug... but there were unresolved references, and so I checked... but the source is well-formed... and finally it hit me:
My folder hierarchy looks like so:
src/
identity/
is.cpp
make.cpp
view.cpp
range/
is.cpp
make.cpp
view.cpp
and so does the hierarchy in the Solution (I always set it up so that it mimicks the "real" folder structure).
And the diagnostic outputs:
Debug\is.obj
Debug\make.obj
Debug\view.obj
Along with a warning which says that the .obj has been passed twice to the linker and that one will be ignored.
Search no more: Visual has neatly flatten my folder hierarchy, and therefore is unable to neatly compile the source.
At the moment, I am simply thinking of renaming the files, that should cover the issue...
... but is there a way to have Visual Studio NOT flatten the file hierarchy ?
I had a similar problem with linker warning LNK4042: object specified more than once; extras ignored. In my case Visual Studio was trying to compile both header and source files with the same name - MyClass.h and MyClass.cpp. It happened because I renamed .cpp file to .h and Visual Studio got confused. I noticed the problem by looking at the compiler logs in the Debug directory. To resolve just remove .h file from the project then add it again.
Just wanted to cross post what I believe to be the answer, if you open the properties for the entire project, and the change the value under C/C++ -> Output Files -> "Object File Name" to be the following:
$(IntDir)/%(RelativeDir)/
Under VS 2010, I believe this will disambiguate all of the object files (as I believe windows won't let you under any crazy circumstances have two files with the same names in the same directory). Please also check out the details here.
Right-click the .cpp file in the Solution Explorer window, Properties, C/C++, Output Files, Object File Name setting. The default is $(IntDir)\, that's what is doing the flattening. All the .obj file will go into $(IntDir), the "Debug" directory in the debug configuration.
You can change the setting, say $(IntDir)\is2.obj. Or select all the files from one group (use Shift+Click) and change the setting to, say, $(IntDir)\identity\
Or you can change the .cpp filename so that .obj files don't overwrite each other. Having files with the exact same name in two directories is a bit odd.
Or you can create multiple projects, creating, say, .lib projects for the files in identity and range. Commonly done in makefile projects for example. That does however make managing the compile and link settings more of a hassle unless you use project property sheets.
Right click on header file -> Property -> ItemType (select C/C++ Header). Do the same with Cpp file but select C/C++ Compiler (it's work for me)
Alternatively to deleting and making a new file you can change the compile/include settings.
Go to your project.vcxproj file, open it with an editor, find the html like line <ItemGroup>.
It should look something like:
<ItemGroup>
<ClCompile Include="implementation.cpp" />
</ItemGroup>
and
<ItemGroup>
<ClInclude Include="declaration.hpp" />
</ItemGroup>`
Assuming your implementation files are .cpp and your declarations are .hpp. Make sure your all your implementation files are listed between the first section if you have more then one and likewise for the second section for multiple declaration files.
I had this problem with stdafx.cpp. Somehow stdafx.cpp got duplicated, so there was a second StdAfx.cpp (mind the different case).
After I removed the StdAfx.cpp everything worked fine!
Using VS 2010.
I use $(IntDir)\%(Directory)\ under C/C++ -> Output Files -> "Object File Name".
I used to have in the same project .c and .cpp files with the same filenames. The files were in folders all over the place and the solutions provided by others created a mess, and folder hell (in my case). Even Release builds would overwrite Debug builds!
A good (not perfect) solution would be to use $(ParentName), but for some reason beyond anyone's grasp it has been removed from later versions of Visual Studio (2015+).
What I use succesfully now is:
$(IntDir)%(Filename)%(Extension).obj
which at least separates .c built object files from .cpp.
I'd like to point out one possible reason for why the ItemType of a .h file would change from C/C++ header to C/C++ compiler:
In the Solution Explorer window of VS (2019 here), right click the project name, choose Add -> New Item;
Select the C++ File (.cpp) template, but type something.h in the name input area, then click OK to add it;
Then you'll encounter the LNK4042 warning if the something.h file be included within more than one .cpp files.
I just overcame a similar error message, and lots more with the procedure below. Symptom: one linker error for every invocation of every function defined in a particular header, plus one at the end of output for every function defined in the header.
Then I remembered that when I had originally created this header, I accidentally had selected "add->new item->c++ file" and though I named it 'whatever.h', it seems Visual Studio considered them both the same kinds of files because of the incorrect action I used to add one. Examining the build output logs made this obvious.
SOLUTION (Using VS Community 2019)
Back up project first (just to be safe).
Right-click the offending header file and select "Exclude from project" (this will not delete them; the VS project will just ignore them).
Do same for the matching .c or .cpp file.
Do Build->Clean on project
Do Build->Rebuild on project
-- there of course will be errors---
Right-click Header Files->Add->Existing Item, then select the .h file
Right-click Source Files->Add->Existing Item, the select the .c or .cpp file
Do Build->Rebuild on project.
This completely cleaned it up for me, relieving me of many irritating linker errors including LNK4042 from the title of this question.
I resolved it changing filenames in my project. There was two files named main.c and main.cpp. I changed one of them and worked.
I'm a real beginner and I'm programming in C++ using Visual Studio.
I've a simple cpp code that recalls some functions written in a .c and .h file. I included that file by means of #include directive and the IDE "sees" the function.
When I compile, I get this
Error 7 error LNK2019: unresolved external symbol _IMUsendAccelToFIFO referenced in function _main D:\Cprojects\Pencil\Pencil\Pencil.obj Pencil
What am I missing here?
Thank you all!
It is a linker error, not a compiler error. The compiler is happy, it saw the declaration of function in the .h file. The linker isn't, it cannot find the definition of the function.
Add the .c file to your project.
If you get an error in Visual Studio you can actually google for the error code and you will get pretty extensive information for that. In this case, googling LNK2019 gives this MSDN page as first hit, which also provides some examples on how you get the error.
Your vendor should have provided some .lib files for you (usually found in a folder named lib?). Make sure that these are added in the project via:
Project > Properties > Configuration Properties > Linker > Input > Additional Dependencies
You could also see if there is any "get started" information for you from your vendor, which explains which dependencies you have to include in your project.
If you feel unsure of what a compiler and what a linker does, pick up a book that explains it, or browse some free alternatives.
Are you using ghettopilot? that's the only reference I can find on the web to the function you're missing. If you are, then you need to include the .lib file for that library in your link options.
Visual Studio will compile .c files as C and .cpp files as C++ by default, and this can cause trouble because if you want to call functions defined in a .c file from a .cpp file, then you must wrap the header in extern "C" { }, as the compiler will expect all functions not declared extern "C" to be from C++. This is because of an implementation detail called name mangling. Alternatively, you could force all files to be compiled as C or as C++ in the project settings.
Solved! Thank you very much!
The libraries I was using needed to be built. I tried but I couldn't build them as I used to get "heap space" error!
I installed Visual Studio 2005 (with which the code was produced by the vendor) and it worked at first attempt! There are probably some back-compatibility issues..