Skip to main content

Command Prompt VS 2010: Fix MSVCRT.lib(MSVCR100.dll) : error LNK2005: xxx already defined in LIBCMT.lib(xxx.obj)

One of my task is to use Command Prompt of Visual Studio 2010 to evaluate the assignments of students. This assignment is about pointer and memory management in C++.
The students are required to use new and delete operator in C++ to manage the memory and they need to ensure that the number of new callings must be equal to the number of delete callings.
I overload the new and delete operator, build it into a static library (.lib file) and require student to use this .lib file when implementing the assignment.
There is no problem when building the .lib file in VS 2010 project, but there are errors when compile the source code if I link the source code to the above static .lib file.
Here is the format of error:
MSVCRT.lib(MSVCR100.dll) : error LNK2005: xxx already defined in LIBCMT.lib(xxx.obj)
Fortunately, I found 2 ways to fix this error on the internet.
The first one is to remove the default lib when compiling the source code, it's libcmt.lib, because this libcmt.lib already defines the operators that I overload. This lib supports multi-thread and of course, it is out of scope of this assignment. So, I feel secure when remove this lib.

cl -o source.cpp xxx.lib /link /nodefaultlib:libcmt.lib
(Note that you must include /link switch to make the prompt switch to linker; otherwise, the error will not be fixed.)

The second one, the better solution, is to change the mode of CRT from static to dynamic mode. This solution solves the root cause of this error.
cl /MD -o source.cpp xxx.lib
I will explain the detail solution next time. Compare between static and dynamic CRT. Why does the lib file use dynamic CRT, not static CRT?
Get some sleep now!
I get this solution from here: http://fixunix.com/programmer/95174-use-nodefaultlib-library.html

Comments

Popular posts from this blog

HTTP_REFERER

A friend of mine ask me if a server can know where we come from before we go to that server (referal link). YES. This information is the field "HTTP_REFERRER" in $_SERVER of the coming request. Here is an example: http://113.161.96.198/referal/ For the reason of SEO, some guys do not want any server know about this referal link that points to their own server. Here are some solutions: HTML5: Add norefer attribute   No REFERRER PHP redirect: <?php header( 'Location: http://113.161.96.198/referal/ ' ) ; ?> .... lot of solutions lol

PHP json_encode return empty array [] instead of empty object {}

Problem: Get below array for example.  $status = array ( "message" => "error" , "club_id" => $_club_id , "status" => "1" , "membership_info" => array (), ); This array will be encoded in json format echo json_encode($status); This function return json: {"message":"error","club_id":275,"status":"1","membership_info": [] } Notice the empty array [], it is not consistent, it should be an empty object {} {"message":"error","club_id":275,"status":"1","membership_info": {} } The cause: This problem is caused by the called function array(), which yields an empty array [] Solution: There are 2 solutions for this issue: Solution 1: Call new stdClass instead of array(), stdClass generates an empty object {} Solution 2: The above solution is complex in case there are plenty of arr

SQL query optimization

SELECT query {EXPLAIN | DESCRIBE | DESC}  We usually use this command to check to see if how many rows the query goes through. Especially for the INDEX column checking. Search LIKE '%key%' consumes more resource than LIKE 'key%' Denormalisation This database design helps to speed up the data reading. Because the SQL query no need to join data between tables.